Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is it possible to open a file that's being used by another process

Status
Not open for further replies.
Oct 15, 2003
145
US
I'm trying to use a file that is used by another program to determine a user's rights to certain databases. When I try to open the file using the following line:

objStreamReader = New StreamReader("\\networkdrive\filename.dat", FileAccess.Read)

I get the following error when running the program:

An unhandled exception of type 'System.IO.IOException' occured in mscorlib.dll. Additional Information: the process cannot access the file "filename" because it's being used by another process.

I looked up this error on microsoft's page - and found that this is an error in xmlvalidatingreader class - but I don't think I'm using that....

When I move the file or make a copy of the file - I don't have a problem opening it. So the file itself is not corrupt - I'm just having problems openeing it if it's being used by something else - is there a way to get around this?

Thanks!!
 
Wyld,

The fault is with the other process having basically 'locked' the file. So it depends on whether the other process can be run without locking the file.
I'm not aware of a way around it but maybe there is one.
 
Are you closing the file after you open it in code?

File.Close

You should have it in the Finally portion of a try catch block.

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb

-----------------------------------

If you can't explain it simply, you don't understand it well enough.
- A. Einstein





 
I have objstreamreader.close -- this is doing the same thing as file.close - right? since I did do a:

objStreamReader = New StreamReader("\\networkdrive\filename.dat", FileAccess.Read) in the beginning of my code

I thought that it was because the other program has the file locked out - but I'm able to open the file - if I find it in windows explorer and double click on it - it's just when I try to open it via my program I get that error.
 
FileAccess refers to what OTHER users may do to the file. Because you did not specify WRITE, you are locked out.
Try
objStreamReader = New StreamReader("\\networkdrive\filename.dat", _
FileAccess.Read Or FileAccess.Write)

The OR combimes the flag attributes/

Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
JohnYingling - I have tried that - it still won't open - I get the same error message.

Maybe it's not possible....

I just don't understand - because when I find the file via windows explorer - I'm able to open it just fine no errors no prompts saying another process has it open...nothing - but when I try to open it via my program - I get the error message.
 
well I'd still like to know if this is a possibility - I have a work around - I make a copy of the file using filestream class - it's slightly time consuming - and would be better if I could just use the orginal file and not have to make a copy...but this works.

 
Just a thought, have you tried using FileOpen and FileGet

Somthing like this

FileOpen(1, sFilePath, OpenMode.Binary)
FileGet(1, sFileContent)
MessageBox.Show(sFileContent)
FileClose(1)

-Kris
 
There is no Stream Constructor that specifes a FileShare enumeration. I assume that you do not have Option Strict On so VB probably coerced the FileShare enumeration to a boolean value.
Public Sub New( _
ByVal path As String, _
ByVal detectEncodingFromByteOrderMarks As Boolean _
)

FileShare is declared on the Open method of a File.
Try the File.Open method
Overloads Public Shared Function Open( _
ByVal path As String, _
ByVal mode As FileMode, _
ByVal access As FileAccess, _
ByVal share As FileShare _
) As FileStream

Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Wyld,

I was just remembering that in VB6 there were cases where we'd have to place an explicit Close prior to opening a file that had been opened and closed in another scope.
It never made sense, and it didn't happen in every case but the workaround was easy so I never bothered to research it.
On the other hand, you are stating that the file is being used by another program. If that program is putting a system-level lock on it, I'd say you're out of luck.
Some DBs are pretty restrictive but it is different that you can copy the file from within .net.

phan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top