473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 6778
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.