views:

360

answers:

2
+2  Q: 

Force open a file

One C++ program is filling a logfile, it keeps a handle to the logfile. I want to open this logfile with a C# program and parse it to see if a certain line has been added to the logfile.

However, if I open the logfile with C# I get a "this file is being used by another process" IOException. Code used:

using(StreamReader reader = File.OpenRead(myFile))//IOException

The C++ program opens the file with (I can not change the C++ program):

m_hFile = tsopen(m_csFilePath, 
_O_WRONLY|_O_APPEND|_O_TRUNC|_O_CREAT|_O_BINARY,
_SH_DENYWR,
_S_IREAD | _S_IWRITE);

Opening the file with notepad works fine so it should be possible to open it. Can I force my C# program to open the file in readonly mode?

+7  A: 

Perhaps try opening the file with the ReadWrite FileShare?

File.Open(myFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Samuel
Cheers, misunderstood the meaning of the FileShare enum.
Carra
+1  A: 

You could take a snapshot copy of the log file using File.Copy and try to open that.

0xA3