By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,958 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,958 IT Pros & Developers. It's quick & easy.

File open fails if the file is being used by another process

P: n/a
Hi,

I'm trying to open a file with read access only, e.g. FileStream fs = new
FileStream("C:\foo.txt", FileMode.Open, FileAccess.Read); The file is open
by another process and the FileStream constructor throws an IOException. I
thought that it was possible to open a file with read access even if it's
being used by another process. Is that not true?

Thanks,
Otik
Jul 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Is that not true?


It depends on how the other process opened the file (see the FileShare
enum).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #2

P: n/a
I believe the other process is a C++ app, but I don't know how it specified
file sharing. However, I can open the file with Notepad and other apps
(while the other process is using it), so I don't think it's denying read
access. Any ideas why I can't open it for reading using my .NET app?

"Mattias Sjögren" wrote:
Is that not true?


It depends on how the other process opened the file (see the FileShare
enum).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Jul 21 '05 #3

P: n/a
use
FileStream fs = new FileStream("C:\foo.txt", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);

That may work for you. The point of using FileShare is that you want to
share the read/write permission with anyone else who needs the file. (It's
counterintuitive, I know).

If the other app didn't lock the file, you should be able to read from it.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Otik" <Ot**@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
Hi,

I'm trying to open a file with read access only, e.g. FileStream fs = new
FileStream("C:\foo.txt", FileMode.Open, FileAccess.Read); The file is open by another process and the FileStream constructor throws an IOException. I thought that it was possible to open a file with read access even if it's
being used by another process. Is that not true?

Thanks,
Otik

Jul 21 '05 #4

P: n/a
Nick,

Thanks for the response. Specifying FileShare.ReadWrite worked! I had
tried just FileShare.Read since I only wanted read access, but I guess you
need to specify write as well since the other process is writing to the file.

Thanks,
Otik

"Nick Malik [Microsoft]" wrote:
use
FileStream fs = new FileStream("C:\foo.txt", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);

That may work for you. The point of using FileShare is that you want to
share the read/write permission with anyone else who needs the file. (It's
counterintuitive, I know).

If the other app didn't lock the file, you should be able to read from it.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Otik" <Ot**@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
Hi,

I'm trying to open a file with read access only, e.g. FileStream fs = new
FileStream("C:\foo.txt", FileMode.Open, FileAccess.Read); The file is

open
by another process and the FileStream constructor throws an IOException.

I
thought that it was possible to open a file with read access even if it's
being used by another process. Is that not true?

Thanks,
Otik


Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.