2011-01-26 77 views
0

我想製作一個Windows Forms應用程序,它將讀取文本文件並將文本文件的字段放入文本框中。文本文件格式的如何使用C#解析文本文件?

例子:

Name;Surname;Birthday;Address 

Name;Surname;Birthday;Address 

的WinForms

Name: textboxname 

Surname: textboxsurname 

Birthday: textboxbirth 

Address: textboxaddress 

我也希望這個WinForms應用程序有一個NextBack按鈕,以便它能通過記錄週期。

我不知道如何在C#中做到這一點。我從哪說起呢?

+3

看看這個:http://stackoverflow.com/questions/1375410/very-simple-c-csv-reader – 2011-01-26 14:31:09

+0

好吧,我認爲那是正確的:-) THX – Sebastian 2011-01-26 14:35:57

回答

2

在一個簡單的形式,你逐行讀取文件中的行,拆分各行;和使用的值:

// open the file in a way so that we can read it line by line 
using (Stream fileStream = File.Open("path-to-file", FileMode.Open)) 
using (StreamReader reader = new StreamReader(fileStream)) 
{ 
    string line = null; 
    do 
    { 
     // get the next line from the file 
     line = reader.ReadLine(); 
     if (line == null) 
     { 
      // there are no more lines; break out of the loop 
      break; 
     } 

     // split the line on each semicolon character 
     string[] parts = line.Split(';'); 
     // now the array contains values as such: 
     // "Name" in parts[0] 
     // "Surname" in parts[1] 
     // "Birthday" in parts[2] 
     // "Address" in parts[3] 

    } while (true); 
} 

此外,檢查出CSVReader這是圖書館促進喜歡這些文件的處理。

+0

很好的答案.. .thx我會嘗試,但我怎麼做下一個和後退按鈕? – Sebastian 2011-01-26 14:40:41

5
foreach (string line in File.ReadAllLines("path-to-file")) 
{ 
    string[] data = line.Split(';'); 
    // "Name" in data[0] 
    // "Surname" in data[1] 
    // "Birthday" in data[2] 
    // "Address" in data[3] 
} 

這比弗雷德裏克的代碼更簡單一點點,但是這一切在一次讀取文件。這通常很好,但會導致非常大的文件問題。

0

基本上你必須

  • 讀取文件(File.ReadAllLines)
  • 創建的記錄列表(1個記錄= 1套姓名,生日)
  • 解析讀文本和填入記錄
  • 創建窗體與採集的數據通過這種形式
  • 創建自己的用戶控件=設置文本框來查看你的數據+幾個按鈕(FW和BW)
  • 名單

這是一個相當複雜的問題。

2

下面是一個簡單的例子,展示瞭如何使用VB.NET TextFieldParser解析CSV文件。爲什麼TextFieldParser?因爲它是那裏最完整的CSV解析器,並且已經安裝在.NET中。

它還顯示數據綁定如何在Windows窗體中工作。閱讀文檔以獲取更多信息,這只是爲了讓你開始。

using System; 
using System.Data; 
using System.Drawing; 
using System.Windows.Forms; 
using Microsoft.VisualBasic.FileIO; 

public class Form1 : Form 
{ 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new Form1()); 
    } 

    DataTable records; 
    BindingSource bindingSource; 

    public Form1() 
    { 
     // Create controls 
     Controls.Add(new Label { Text = "Name", AutoSize = true, Location = new Point(10, 10) }); 
     Controls.Add(new TextBox { Name = "Name", Location = new Point(90, 10) }); 
     Controls.Add(new Label { Text = "Sirname", AutoSize = true, Location = new Point(10, 40) }); 
     Controls.Add(new TextBox { Name = "Sirname", Location = new Point(90, 40) }); 
     Controls.Add(new Label { Text = "Birthday", AutoSize = true, Location = new Point(10, 70) }); 
     Controls.Add(new TextBox { Name = "Birthday", Location = new Point(90, 70) }); 
     Controls.Add(new Label { Text = "Address", AutoSize = true, Location = new Point(10, 100) }); 
     Controls.Add(new TextBox { Name = "Address", Location = new Point(90, 100), Size = new Size(180, 30) }); 
     Controls.Add(new Button { Name = "PrevRecord", Text = "<<", Location = new Point(10, 150) }); 
     Controls.Add(new Button { Name = "NextRecord", Text = ">>", Location = new Point(150, 150) }); 

     // Load data and create binding source 
     records = ReadDataFromFile("Test.csv"); 
     bindingSource = new BindingSource(records, ""); 

     // Bind controls to data 
     Controls["Name"].DataBindings.Add(new Binding("Text", bindingSource, "Name")); 
     Controls["Sirname"].DataBindings.Add(new Binding("Text", bindingSource, "Sirname")); 
     Controls["Birthday"].DataBindings.Add(new Binding("Text", bindingSource, "Birthday")); 
     Controls["Address"].DataBindings.Add(new Binding("Text", bindingSource, "Address")); 

     // Wire button click events 
     Controls["PrevRecord"].Click += (s, e) => bindingSource.Position -= 1; 
     Controls["NextRecord"].Click += (s, e) => bindingSource.Position += 1; 
    } 

    DataTable ReadDataFromFile(string path) 
    { 
     // Create and initialize a data table 
     DataTable table = new DataTable(); 
     table.Columns.Add("Name"); 
     table.Columns.Add("Sirname"); 
     table.Columns.Add("Birthday"); 
     table.Columns.Add("Address"); 

     // Parse CSV into DataTable 
     using (TextFieldParser parser = new TextFieldParser(path) { Delimiters = new String[] { ";" } }) 
     { 
      string[] fields; 
      while ((fields = parser.ReadFields()) != null) 
      { 
       DataRow row = table.NewRow(); 
       for (int n = 0; n < fields.Length; n++) 
        row[n] = fields[n]; 
       table.Rows.Add(row); 
      } 
     } 

     return table; 
    } 
}