2013-10-05 208 views
1

我正在使用Winform應用程序,它將員工數據作爲4個元素的數組 ,並將這些數據正確保存在一行中的文本文件中,並與SEPARATOR(「,」)..從txt文件讀取每行數組

我的問題是如何使它加載任何行數據並識別分隔符(「,」) ,以便我可以通過第一個名稱讀取所有數據?

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 

回答

3

你可以簡單地再次讀取該文件,並在您選擇

 var textLines = File.ReadAllLines(""); 

     foreach (var line in textLines) 
     { 
      string[] dataArray = line.Split(','); 
     } 
+0

我沒有和跟隨它用行動F11,成功得到了所有項目,但..仍然沒有將這些數據寫入到文本框中的! –

1

的分隔符使用分割終於摸索,線分離到陣列中的數據,在右邊的文本框中添加 每:

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]); 
       } 
      } 
     } 

非常感謝社社長偉利

0
[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");