Reputation: 61
I have a file as c:\sai\chn_20151019_5932.txt . Here 5932 is minutes and seconds format. 59 mins 32 secs. When we run again the package on the same day the existed file in the folder should deleted but , due to the seconds I am unable to delete the file. I need C# code something like this.
steing filename: @"c:\sai\chn_20151019_5932.txt";
if(filename.exists(@"c:\sai\chn_20151019" + "*")
{
delete.file(filename);
}
Upvotes: 0
Views: 1594
Reputation: 61912
Use using System.IO;
, then:
foreach (var f in Directory.GetFiles(@"c:\sai\", "chn_20151019*.txt"))
{
File.Delete(f);
}
If you like to be fancy (using System;
):
Array.ForEach(Directory.GetFiles(@"c:\sai\", "chn_20151019*.txt"), File.Delete);
Upvotes: 0
Reputation: 172378
Try this:
var dir = new DirectoryInfo(@"c:\sai\");
foreach (var file in dir.EnumerateFiles("chn_20151019*.txt")) {
file.Delete();
}
EDIT:
DirectoryInfo di = new DirectoryInfo(@"c:\sai\");
FileInfo[] files = di.GetFiles("chn_20151019*.txt")
.Where(p => p.Extension == ".txt").ToArray();
foreach (FileInfo file in files)
try
{
File.Delete(file.FullName);
}
catch { }
Upvotes: 1
Reputation: 2221
if you are using .NET or above -
string path = @"c:\sai";
bool exist = Directory.EnumerateFiles(path, "chn_20151019*").Any();
Upvotes: 0
Reputation: 650
You can use EnumerateFiles
which will find files by pattern in specified directory. And then just use File.Delete
Directory.EnumerateFiles("c:\\sai","chn_20151019*",SearchOption.TopDirectoryOnly)
.ToList()
.ForEach(x=>File.Delete(x));
Upvotes: 0