9

C# how can I list the files in the folder in listview?

 2 years ago
source link: https://www.codeproject.com/Questions/5332288/Csharp-how-can-I-list-the-files-in-the-folder-in-l
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

See more:

I want to list the files in the folder in listview

I tried this code

Copy Code
DirectoryInfo di = new DirectoryInfo(@"C:\Users\Yusuf\Desktop\excel");
Dosyalar = di.GetFiles("*.csv");

ListViewItem item = new ListViewItem();

foreach (FileInfo file in Dosyalar)
{
    for (int i = 1; i < Dosyalar.Length; i++)
    {
        item.SubItems.Add(file.Name);
        item.Text = i.ToString();
        listView2.Items.Add(item);
    }
}


I'm getting result like this

Copy Code
1 mytest.csv
2 mytest.csv
3 mytest.csv
1 mytest1.csv
2 mytest1.csv
3 mytest1.csv
1 mytest2.csv
2 mytest2.csv
3 mytest2.csv



I want to get result like this

Copy Code
1 mytest.csv
2 mytest1.csv
3 mytest2.csv



What I have tried:

Copy Code
foreach (FileInfo file in Dosyalar)
{
    ListViewItem item = new ListViewItem();
    for (int i = 1; i < Dosyalar.Length; i++)
    {
        item.SubItems.Add(file.Name);
        item.Text = i.ToString();
        listView2.Items.Add(item);
    }
}
Copy Code
ListViewItem item = new ListViewItem();
foreach (FileInfo file in Dosyalar)
    {
        item.SubItems.Add(file.Name);
    }

for (int i = 1; i < Dosyalar.Length; i++)
    {
        
        item.Text = i.ToString();
        
    }
        listView2.Items.Add(item);

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK