tags:

views:

285

answers:

4

Hi all. I'm writing a C# app to read a file that another application holds open. As some of you may guess, all I get is IOExceptions because "the file is being used by another process". I've tried tweaking File.Open() a little; this is my current try:

FileStream fsIn = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);

(I know that the FileShare flag is more meaningful for other processes that will access the file thereafter, but I have tried with it anyway.)

What puzzles me is that Notepad.exe will open the file just fine. Before I dig into Filemon for more clues, does any of you know how to open the file the way Notepad does? Thanks.

+3  A: 

You almost have it. You actually want FileShare.ReadWrite:

FileStream fsIn = File.Open(fileName,
                            FileMode.Open,
                            FileAccess.Read,
                            FileShare.ReadWrite);

FileShare.Read disallows other processes from writing to that file. FileShare.ReadWrite allows this.

Write Allows subsequent opening of the file for writing. If this flag is not specified, any request to open the file for writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.

ReadWrite Allows subsequent opening of the file for reading or writing. If this flag is not specified, any request to open the file for reading or writing (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.

It's backwards to me as well.

Michael Petrotta
A: 

Try:

FileStream file = new FileStream(filename, FileMode.Open);
waqasahmed
Unfortunately it doesn't behave any better! Thanks anyway.
Humberto
+3  A: 

Well, if you are dealing with small files, just use

string myFileContents = System.IO.File.ReadAllText(@"c:\daniellesteele.txt");

for text or

byte[] myfileContents = System.IO.File.ReadAllBytes(@"c:\aleagueoftheirmoan.mpg");

for binary files.

Dave Markle
tempted to give +1 for the mpg :P
Erik van Brakel
...but only tempted.
Robert Harvey
interesting filename.... :P
nstehr
Thanks for the tip, but this is the kind of file that starts small but may grow very big quickly!
Humberto
yeah, then this is NOT the option for you. Just throwing it out there for general information. I love me some ReadAllText(). And you guys are right. This answer definitely does not deserve a +1. :P
Dave Markle
+1  A: 

Are you sure Notepad can open the file after the other process has opened it? It looks like it's up to who initially owns the file if sharing is granted. See the following example from MSDN:

The following FileStream constructor opens an existing file and grants read-only access to other users (FileShare.Read).

[C#] 
FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);

Of course I'm not an OS expert, so this may be completely wrong.

xanadont