472,968 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,968 software developers and data experts.

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

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
4 6753
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Pekka Niiranen | last post by:
Hi, I have used the following example from win32 extensions: -----SCRIPT STARTS---- import win32file import win32con import win32security import pywintypes
4
by: Jerry | last post by:
I've inherited a system w/5 projects - I'll refer to them as P1 thru P5 - P5 being the executable... P1 has no dependents. P2 is dependant upon P1 P3 has no dependents. P4 is dependant upon P1,...
6
by: BenSimmons | last post by:
Hi i'm creating a program that reads various log files from another program (Find-a-Drug DC project). The problem is, when I read some of the logs I get the error This process cannot access the...
4
by: Otik | last post by:
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...
0
by: daniele.balducci | last post by:
Hi All, I'm experiencing troubles in a simple .net web application. The applicatione is made up by a single form looping to move some files to a different folder . the main loop is as follows...
7
by: RSH | last post by:
Hi, I have an ASP .Net web page that creates a temp directory on the server then it is using the File.Copy command to move a file to a temp download directory, Then I am doing a...
17
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: ...
5
by: bulldog8 | last post by:
I've read numerous posts and have tried multiple approaches that I found, but just cannot get a file renamed. I am using VB.Net 2002 ... Here is what I have tried: 1) Code common to all...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.