0

how can I delete all files of a specific pattern except the present file that is using? I have tried like:

string temp_file_format = "ScriptLog_" + DateTime.Now.ToString("dd_MM_yyyy_HH") + "*";
System.IO.DirectoryInfo dir = new DirectoryInfo(path);
            System.IO.FileInfo[] files = dir.GetFiles(temp_file_format);
            foreach (FileInfo file in files)
            {
                file.Delete();
            }

Suppose If I have 3 files with time stamp of 12:08, 12:09 and 12:12 and my application curently uses 12:15 log file, then it should delete the above 3 logs. Now what it does is that, it deletes the presently using file also..

any help would be really appreciated..

4

1 回答 1

1
var files = dir.GetFiles(temp_file_format).OrderByDescending(f=>f.CreationTime).Skip(1).ToList();

for( int i=0; i< files.Count; i++)
   files[i].Delete();
于 2013-09-13T11:19:37.340 回答