C # Read and write files When the file is used by another process, so the process cannot access the file.

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);

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()+"
"); }

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);

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()+"
"); }

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3060 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.