Reading an opened file in C#

  • Thread starter Thread starter Tim Royal
  • Start date Start date
T

Tim Royal

I have a situation where I have an application, written by another party in
C++, that logs specific information to a log file. I am trying to open and
read this file in C# while the application is running.

The problem is I'm getting a sharing violation when attempting to open the
file. I recall that there was a flag or setting that could be used to open
files even when they're in use by another application, but I may be
mistaken.

Does anyone know of such a flag (I didn't see one in the documentation)? Or
does anyone know of a more preferred manner with which to accomplish this?

Thanks for any insight.

Tim
 
Thanks for the info, and the lead on the enumeration. I'll investigate hgow
they're opening the file from the application.

Tim


Peter Duniho said:
I have a situation where I have an application, written by another party
in
C++, that logs specific information to a log file. I am trying to open
and
read this file in C# while the application is running.

The problem is I'm getting a sharing violation when attempting to open
the
file. I recall that there was a flag or setting that could be used to
open
files even when they're in use by another application, but I may be
mistaken. [...]

It depends on how the file was opened by the other application. If it
opened the file with a sharing mode that would allow shared access, then
as long as you use a compatible sharing mode, you can also open the file.

A common default though is complete exclusive access to the file. It's
possible that there's no way for your application to open the file until
the other application is done with it.

See the FileShare enumeration for more details:
http://msdn.microsoft.com/en-us/library/system.io.fileshare.aspx

Pete
 
I have a situation where I have an application, written by another party in
C++, that logs specific information to a log file. I am trying to open and
read this file in C# while the application is running.

The problem is I'm getting a sharing violation when attempting to open the
file. I recall that there was a flag or setting that could be used to open
files even when they're in use by another application, but I may be
mistaken.

Does anyone know of such a flag (I didn't see one in the documentation)? Or
does anyone know of a more preferred manner with which to accomplish this?

Thanks for any insight.

Tim

Hi,

You can do it IF AND ONLY IF the other app opened the file with access
to be shared at least for read.
If it does then you can read it with shared reading access.

Take a look at the version of FileStream constructor that receives a
FileAcces, FileMode and FileSharing parameters
 
Maybe a daft idea, but what about making a copy of the file and then
manipulating the copied file. It might not be a practical solution,
just a stab in the dark.
 
Back
Top