I want to search for the characters in Name file of the Directory. If the content is different from the results found, delete the file. I've tried the code below:
private void btnStart_Click(object sender, EventArgs e) { FileWithFilter(Parth, "Abc123"); } private void FileWithFilter(string folderName, string filesToExclude) { DateTime dateTime = DateTime.Now; DirectoryInfo dir = new DirectoryInfo(folderName); foreach (FileInfo fi in dir.GetFiles()) { if (!fi.Name.Contains(filesToExclude)) { fi.delete(); } } } It works, but if the file in Directory is more than 10 or 11(file), code can't run correctly! please tell me any better way or other solution.
!fi.Name.Contains(filesToExclude, StringComparison.OrdinalIgnoreCase).