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.
  • 165,803
  • 30
  • 257
  • 277
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
  • 54,517
  • 9
  • 84
  • 106
0
DirectoryInfo directory = new DirectoryInfo(path);
FileInfo[] files = directory.GetFiles("*.log");
vino20
  • 419
  • 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,341
  • 4
  • 23
  • 34