Reading a used file
-
Hi. I have a batch-file that creates a logfile. My C# Programm should read this file even if this will still be updated. The (wonderful) editor "Textpad" can easily open that file ... my C# Program can't .. I get 'file is used by another process' Error. I already tried :
FileStream output = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.Read, 4196, true);
but I still get the error. Can anyone help me PLEASE ?? :confused: -
Hi. I have a batch-file that creates a logfile. My C# Programm should read this file even if this will still be updated. The (wonderful) editor "Textpad" can easily open that file ... my C# Program can't .. I get 'file is used by another process' Error. I already tried :
FileStream output = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.Read, 4196, true);
but I still get the error. Can anyone help me PLEASE ?? :confused:Try using: File myFile = File.Open(...) using the version that takes a FileShare enumeration, and then create the FileStream from that.
-
Try using: File myFile = File.Open(...) using the version that takes a FileShare enumeration, and then create the FileStream from that.
Hi Eric. I tried, like you said --- but this results in an error (File.Open returns a FileStream !). He can't convert from FileStream to File .. so I assumed you meant :
FileStream myfile = File.Open(FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.Read);
But this also causes the exception as before. Or did I misunderstood you ? -
Hi Eric. I tried, like you said --- but this results in an error (File.Open returns a FileStream !). He can't convert from FileStream to File .. so I assumed you meant :
FileStream myfile = File.Open(FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.Read);
But this also causes the exception as before. Or did I misunderstood you ?When you specify sharing, you need to match the sharing that the file was opened with. In my tests, using FileShare.ReadWrite worked.
-
When you specify sharing, you need to match the sharing that the file was opened with. In my tests, using FileShare.ReadWrite worked.
Like you said : It works ! I misunderstood the FileShare .. I thought that was for my access. Thank you very much !