Reputation: 159
I have a pretty little piece of code that will loop through a networked drive and return the file name and extension of all files. I have the output set to display in a listbox. My current conundrum is that it pulls in temp files "~filename.ext" and this is throwing off my count. How do I go about directing it to ignore all temp files "~filename.ext".
string[] filePaths = Directory.GetFiles(@"\\server\directory\folder\folder\", "*.xlsm",
SearchOption.AllDirectories);
statusCodeLB.Items.Clear();
foreach (string file in filePaths)
{
statusCodeLB.Items.Add(Path.GetFileName(file));
}
statusLabel.Text = statusCodeLB.Items.Count.ToString();
Upvotes: 1
Views: 31
Reputation: 27861
You can use an if
statement in the loop to check whether the file name starts with "~" like this:
//...
foreach (string file in filePaths)
{
string filename = Path.GetFileName(file);
if(filename.StartsWith("~"))
continue; //Skip
statusCodeLB.Items.Add(filename);
}
//...
Upvotes: 1