2013-05-03 39 views

回答

3

好吧,如果你的行中包含你需要的一切,並不需要拆分之後,你可以用它代替的ListView列表框

foreach(string line in File.ReadAllLines(pathToYourFile)) 
    ListBox.Items.Add(line); 

或者,如果你真的需要ListView控件,您可以使用

foreach(string line in File.ReadAllLines(pathToYourFile)) 
    listView.Items.Add(new ListViewItem(line)); 
+0

非常感謝,問題解決了。 – user2323554 2013-05-03 17:23:25

2

使用iostreamreader ..然後使用輸入行功能..然後填寫列表視圖

2

嘗試是這樣的:

string[] lines = System.IO.File.ReadAllLines(@"yourtextfile"); 
foreach (string line in lines) 
{ 
    listView1.Items.Add(line); 
} 
1

這是您的Linq示例。

using System.Linq; 

...  

System.IO.File.ReadAllLines(pathToFile) 
    .ToList() 
    .ForEach(line => listView.Items.Add(new ListViewItem(line))); 
0

首先,

using System.IO; <-- to read the file 

然後,如果你可以使用一個列表框,工作的AddRange沒有良好的循環:

listBox1.Items.Clear(); 
string[] s_array = File.ReadAllLines(-- your file path --); 
listBox1.Items.AddRange(s_array); 

如果您使用的是列表視圖,那麼上面的Blablablaster建議的循環運行良好

相關問題