Reading and disposal of text files
-
I'm trying to write a program that checks the text files, and if they contain a certain sequence of symbols, removes them. The problem is, when removed, there's a mistake "process can't get access to the file, that's the file being used in another process." I didn't even open the file myself.
Here's the code:
try { var files = from file in Directory.EnumerateFiles( @"c:\test\", "*.txt", SearchOption.AllDirectories) from line in File.ReadLines(file) where line.Contains("NoOneLivesForever") select ne { File = file, Line = line };
foreach (var f in files) { Console.WriteLine("{0}\t{1}", f.File, f.Line); System.IO.File.Delete(f.File); } Console.WriteLine("{0} files found.", files.Count().ToString());
}
catch (UnauthorizedAccessException UAEx)
{
Console.WriteLine(UAEx.Message);
}
catch (PathTooLongException PathEx)
{
Console.WriteLine(PathEx.Message);
}
-
File.ReadLines
We need to replace byFile.ReadAllLines
♪
OtherwiseFile.Delete
Called when the file is open at the timeFile.ReadLines
♪