设置一个字符串,等于.txt文件的行数

本文关键字:txt 等于 文件 字符串 一个 设置 | 更新日期: 2023-09-27 18:35:46

我在尝试弄清楚如何设置等于.txt文件的某些行的字符串变量时遇到了一个小问题。我正在尝试做的是设置ContactInformatiom.ContactName ContactInformation所有的东西等于我保存定义的.txt文件的不同行,但我似乎无法弄清楚如何做到这一点。我尝试了各种各样的东西,但似乎都没有奏效,我看遍了这个网站和其他网站,没有遇到和我有同样问题的人,我不确定这是我的措辞方式还是什么。

我正在使用以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Contact_List
{
public partial class Form1 : Form
{
    string contactpath = @"CurrentContacts.txt";
    string contactpath2 = @"" + ContactInformation.ContactName + ".txt";
    public Form1()
    {
        if(!File.Exists(contactpath))
        {
            File.Create(contactpath, 1024).Dispose();
        }
        InitializeComponent();
    }
    private void AddContactButton_Click(object sender, EventArgs e)
    {
        NewContactTextBoxName.Visible = true;
        NewContactTextBoxCell.Visible = true;
        NewContactName.Visible = true;
        NewContactPhoneNumberOther.Visible = true;
        NewContactSaveButton.Visible = true;
        NewContactSaveText.Visible = true;
        textBox6.Visible = true;
        textBox5.Visible = true;
        textBox4.Visible = true;
    }
    private void NewContactSaveButton_Click_1(object sender, EventArgs e)
    {
        if (NewContactTextBoxName.Text == "")
        {
            MessageBox.Show("Please enter a Contact Name");
        }
        else
        {
            ContactInformation.ContactName = NewContactTextBoxName.Text;
            string contactpath2 = @"" + ContactInformation.ContactName + ".txt";
            string Readfile = File.ReadAllText(contactpath);
            if (Readfile.Contains(NewContactTextBoxName.Text))
            {
                MessageBox.Show("This contact already exsists. Please rename the contact you're trying to create or delete the exsisting contact");
            }
            else
            {
                comboBox1.Items.Add(ContactInformation.ContactName);
                File.Create(contactpath2).Dispose();
                using (StreamWriter Writeline = new StreamWriter(contactpath, true))
                {
                Writeline.WriteLine(ContactInformation.ContactName);
                Writeline.Dispose();
                }
                using (StreamWriter Writeline2 = new StreamWriter(contactpath2, true))
                {
                Writeline2.WriteLine(NewContactTextBoxName.Text);
                Writeline2.WriteLine(NewContactTextBoxCell.Text);
                Writeline2.WriteLine(NewContactPhoneNumberOther.Text);
                Writeline2.WriteLine(textBox6.Text);
                Writeline2.WriteLine(textBox5.Text);
                Writeline2.WriteLine(textBox4.Text);
                Writeline2.Dispose();
                }
                NewContactName.Visible = false;
                NewContactPhoneNumberOther.Visible = false;
                NewContactSaveButton.Visible = false;
                NewContactSaveText.Visible = false;
                textBox6.Visible = false;
                textBox5.Visible = false;
                NewContactTextBoxName.Visible = false;
                NewContactTextBoxCell.Visible = false;
                textBox4.Visible = false;

            }
        }
    }
    private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
    {
    string currentselection = @"" + comboBox1.SelectedItem.ToString() + ".txt";
    File.ReadAllLines(currentselection);
    //set Contactinformation.ContactName = to line one of the .txt file
    //set ContactInformation.ContactCell = to line two of the .txt file
    //etc.

        }
    }
}

和以下类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contact_List
{
public class ContactInformation
{
    public static string ContactName;
    public static string ContactCellNumber;
    public static string ContactOtherNumber;
    public static string ContactRelationship;
    public static string ContactEmail;
    public static string ContactNote;
    }
}

如果您需要任何其他信息或澄清我所问的内容,请询问和如果我对自己想要做的事情不够清楚,请提前道歉。

设置一个字符串,等于.txt文件的行数

尝试:

string[] contactInfo = File.ReadAllLines(currentselection);
Contactinformation.ContactName = contactInfo[0];
Contactinformation.ContactCell = contactInfo[1];
.
.
.
.
.

确保在访问元素之前检查数组长度