2013-05-03 1 views
0

txt 파일에서 listView로 문자열을로드하는 방법은 무엇입니까? 3 줄짜리 txt 파일이 있습니다. txt 파일의 첫 번째 행을 listView의 첫 번째 행으로 읽습니다.C# listView에 문자열을 읽는 방법은 무엇입니까?

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

답변

3

당신의 라인은 당신이 필요로하는 모든 것을 포함하고 분할 할 필요가 없습니다 잘 경우 :

+0

매우 고마워, 문제가 해결되었습니다. – user2323554

2

사용 iostreamreader .. 다음의 readline 기능을 사용 .. 다음 목록보기

2

이런 식으로 뭔가를 시도 채우기 대신의 ListView의 목록 상자를 사용할 수 있습니다 후 당신이 정말로 목록보기 필요한 경우

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

은 또는 당신은

을 사용할 수 있습니다
foreach(string line in File.ReadAllLines(pathToYourFile)) 
    listView.Items.Add(new ListViewItem(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에서 제안한 루프가 잘 작동합니다.

관련 문제