C # I/O file reading problem: Another process was developed before and encountered an error in I/O processing.
The file "D: \ log \ cargoabc \ logfilecargoabc.txt" is being used by another process. Therefore, this process cannot access the file.
Logfilecargoabc.txt is a log file.ProgramWrite logs to it.
I/O streams need to be used to read log files and display them in log query.
1. filestream FS = file. openread (URL );
Streamreader sr = new streamreader (system. Io. Stream) FS, system. Text. encoding. Default );
Error message: the file "D: \ log \ cargoabc \ logfilecargoabc.txt" is being used by another process. Therefore, this process cannot access the file.
2. streamreader sr = file. opentext (URL );
Error prompt: the error message "D: \ log \ cargoabc \ logfilecargoabc.txt" is being used by another process. Therefore, this process cannot access the file.
3. filestream FS = new filestream (URL, filemode. Open, fileaccess. Read, fileshare. readwrite );
Streamreader sr = new streamreader (FS, system. Text. encoding. Default );
Read correctly.
Summary:
In this case, you do not only need to open the TXT file in read-only mode, but also need to share the lock. You must also select the flieshare mode as readwrite. Because there are other programs to write to it at any time.