Carra
Carra

Reputation: 17964

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?

Upvotes: 3

Views: 2471

Answers (2)

Dirk Vollmar
Dirk Vollmar

Reputation: 176169

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

Upvotes: 1

Samuel
Samuel

Reputation: 38346

Perhaps try opening the file with the ReadWrite FileShare?

File.Open(myFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)

Upvotes: 8

Related Questions