简体   繁体   中英

c#, having issue on stream reading the msbuild log file while msbuild logger is still writing to it

I try to use StreamReader to read the MSBuild log file real time by using FileSystemWatcher. It always prompt me File Exception when MSBuild is still writing to it. My code may only managed to show the MSBuild log content to my RichTextBox control when MSBuild completed the compilation...

Is there any alternative to write a code for reading a file which is now using by another process? Such as, notepad.exe read file in anytime without concern...

How are you instantiating the StreamReader?

If you are simply passing the file path into the constructor then that may be your problem as doing so will not specify the correct file sharing rights.

Try this:

StreamReader streamReader = new StreamReader(File.Open("C:\\......", FileMode.Open, FileAccess.Read, FileShare.ReadWrite));

You can try to use one of the overloads of the 'Open' method, to specify you are only trying to read. Like this:

FileStream fs = 
    File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM