Now you need to read the log file and display it in the log query, and you need to use the IO stream.
1,
FileStream fs = File.OpenRead(url);
StreamReader sr = new StreamReader((System.IO.Stream)fs, System.Text.Encoding.Default); pre>
Error prompt: The file "D:\Log\log20170317.txt" is being used by another process, so the process cannot access the file.
2,
StreamReader sr = File.OpenText(url);
Error prompt: error prompt : The file "D:\Log\log20170317.txt" is being used by another process, so the process cannot access the file.
3,
string textInfo = File.ReadAllText(filePath,Encoding.Default);
Error prompt: Error prompt: The file "D:\Log\log20170317.txt" is being used by another process, so the process cannot access the file.
4. Correct reading method
FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default); StringBuilder sb = new StringBuilder(); while (!sr.EndOfStream) {sb.AppendLine(sr.ReadLine()+"
"); } span>
Summary:
In such a situation, not only does the txt file need to be opened in read-only mode, but also a shared lock is required. You must also select the flieShare method as ReadWrite. Because there are other programs writing to it at any time.
Quotation: c# The file is being used by another process when reading and writing files, so the process cannot access the file
FileStream fs = File.OpenRead(url);
StreamReader sr = new StreamReader((System.IO.Stream)fs, System.Text.Encoding.Default); pre>StreamReader sr = File.OpenText(url);string textInfo = File.ReadAllText(filePath,Encoding.Default);
FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default); StringBuilder sb = new StringBuilder(); while (!sr.EndOfStream) {sb.AppendLine( sr.ReadLine()+"
"); }