0

I need to add the name of file to my list view which is .log type. I have something like this but it shows only path to that file. My code:

string[] files = Directory.GetFiles(@"C:\...","*.log");

foreach (string file in files)
{
   listView1.Items.Add(file);
}

Any thoughts?

Alex K.
  • 171,639
  • 30
  • 264
  • 288
franzp
  • 109
  • 1
  • 2
  • 15

3 Answers3

2

Depending on your requirements, you can use either Path.GetFileName and Path.GetFileNameWithoutExtension:

string[] files = Directory.GetFiles(@"C:\...","*.log");

foreach (string file in files)
{
   string name = Path.GetFileName(file);
   listView1.Items.Add(name);
}
Andrei
  • 55,890
  • 9
  • 87
  • 108
0
DirectoryInfo directory = new DirectoryInfo(path);
FileInfo[] files = directory.GetFiles("*.log");
vino20
  • 429
  • 4
  • 13
0

Another way to solve this probelm :

const string ExtExe = ".log";
const string MaskExe = "*" + ExtExe;

var files = Directory.GetFiles(dir, MaskExe).Where(item => item.ToLower().EndsWith(ExtExe));
foreach(string file in files) 
{
    put your logic....
}
Anurag Jain
  • 1,371
  • 4
  • 23
  • 34