【问题标题】:Reading from txt file each line as Array从 txt 文件中读取每一行作为数组
【发布时间】:2015-09-25 19:51:16
【问题描述】:

我正在开发 Winform 应用程序,该应用程序将员工数据作为 4 个元素的数组 并将这些数据正确保存到带有分隔符(“,”)的一行中的文本文件中..

我的问题是如何让它加载任何行数据并识别分隔符(“,”) 这样我就可以让它通过名称的第一项读取所有数据?

public partial class Form1 : Form
    {
        string[] data = new string[4];
        string name;
        string job;
        string salary;
        string join;

        #region Save


void save()
    {
        if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
        {
            MessageBox.Show("Please Fill All Fields", "error");
        }

        FileStream file = new FileStream("info.txt", FileMode.Append, FileAccess.Write);
        StreamWriter wr = new StreamWriter(file);
        wr.WriteLine(String.Join(",", data));
        wr.Flush();
        wr.Close();
        comboBox1.Items.Add(data[0]);
        textBox1.Clear();
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();

    }
    #endregion

    #region Search
    void search()
    {
        FileStream file = new FileStream("info.txt", FileMode.Open, FileAccess.Read);
        StreamReader sr = new StreamReader(file);
        sr.ReadLine(string.//what should i do here?
        string[] lines = File.ReadAllLines("info.txt");
        data[0].CompareTo(comboBox1.SelectedItem);
        sr.ReadLine();
        if (data[0] == name)
        {
            textBox1.Text = (data[0]);
            textBox2.Text = (data[1]);
            textBox3.Text = (data[2]);
            textBox4.Text = (data[3]);
        }
    } 
    #endregion  

【问题讨论】:

    标签: c# streamreader


    【解决方案1】:

    您可以简单地再次读取文件,然后在您选择的分隔符上使用拆分

            var textLines = File.ReadAllLines("");
    
            foreach (var line in textLines)
            {
                string[] dataArray = line.Split(',');
            }
    

    【讨论】:

    • 我使用 F11 完成并遵循了它的操作,成功获取了所有项目,但是.. 仍然没有将这些数据写入文本框!!
    【解决方案2】:

    终于成功了,线分割成数组数据, 每个都添加在右侧的文本框中:

    void search()
            {
                FileStream file = new FileStream("info.txt", FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(file);
                sr.ReadLine();
                var textLines = File.ReadAllLines("info.txt");
    
                foreach (var line in textLines)
                {
                    string[] dataArray = line.Split(',');
                    dataArray[0].CompareTo(comboBox1.SelectedItem);
                    if (dataArray[0] == comboBox1.SelectedItem.ToString())
                    {
                       textBox1.Text = (dataArray[0]);
                       textBox2.Text = (dataArray[1]);
                       textBox3.Text = (dataArray[2]);
                       textBox4.Text = (dataArray[3]); 
                    }
                }
            } 
    

    非常感谢李俊伟先生

    【讨论】:

      【解决方案3】:
      [DllImport("kernel32")]
              private static extern int GetPrivateProfileString(string section,
                       string key, string def, StringBuilder retVal,
                      int size, string filePath);
       /// <summary>
              /// read value from given section and key
              /// </summary>
              /// <param name="Section">string</param>
              /// <param name="Key">string</param>
              /// <returns>string</returns>
       public string IniReadValue(string Section, string Key)
              {
                  StringBuilder temp = new StringBuilder(255);
                  int i = GetPrivateProfileString(Section, Key, "", temp,
                                                  255, this.path);
                  return temp.ToString();
      
              }
      

      要调用这个函数见下面的代码

       string[] sBENCHNO=new Strin[256];
           sBENCHNO = ini.IniReadValue("Sextion", "Key");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-03
        • 1970-01-01
        • 2011-09-03
        • 1970-01-01
        • 1970-01-01
        • 2019-02-05
        • 2021-08-02
        相关资源
        最近更新 更多