The process cannot access the file because it is being used by another process

Hey all

This is annoying, I want to perform a copy or move or read operation on a file but the file is already open in word or notepad, whatever.

No when I try for example to rename the file that is open in word the system tells me that I can't do it since word holds it.

How can I do the same in C#, meaning checking if the file is open and alert which application holds it.

Itzik

[414 byte] By [ItzikKatzav] at [2007-12-25]
# 1

Hi,

What you can do is trying to aquire an exclusive share on the file, if you success it means that the file is not used by any other process.

Use the FileShare enumerator when you open the file, therefore your steps should be:

1) Try to open a file with None sharing

2) Close the handle (otherwise not even you can move it :-)

3) If there is no exception you can move the file.

Hope this helps.

Regards

Salvador at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2

I guess you mean something like that

public bool CheckIfFileIsBeingUsed(string fileName){

try{

File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None);

}

catch (Exception exp){

return true;

}

return false;

}

OK

Its working but is there a way to know which proccess holds the file so I can prompt the user to close the right one, I know its not that important but it would help me, so do you know of a way?

Itzik

ItzikKatzav at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3

Hi,

I am not aware of any function to do so, indeed when you try to delete a file in windows that is used by another process it also states that another process is using the file but it does not say to you which one.

Sorry,

Salva

Salvador at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 4
Be careful doing it this way, you'll think the file is locked even when it doesn't exist. Instead of catching System.Exception, catch System.IO.IOException...

nobugz at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 5

Hey...

I have the same problem, but I want to open the file for read - event if it's already opened by another process...

Thanks.

dMeital at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 6
I'd like to know the answer to this too (open a file for read that is already open by an external process).

As a workaround I copied the file (File.Copy(.....) ) and opened the copy. Its really annoying since my text editor can open a file that is currently locked by another process, the copy process *may* be reading the bytes when copying, but C# won't let me open for read access the very same file.

StressedDeveloper at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 7

I came accros this problem too...

I'm trying to send a file from the Server i worte and I am SURE that no program use this file

and still i cant open the file in order to send it away, lets say that a program is using this file (its a BMP)

even then, Why we can't Open file for READ ONLY even if it is Locked.

FileOpen(hIn, sFile, OpenMode.Binary, OpenAccess.Read)

Thanks ahead, Alon.

AlonShimony at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 8
The Bitmap class has a special problem with file locking. GDI+ puts a lock on the file after loading it. Check my code in this thread for a work-around.
nobugz at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 9

Thanks!

that solved that issue

Alon.

AlonShimony at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 10

When I tried your code, it does determine if the file is locked, but in doing so, it locks the file. I modified your code slightly to fix this.

Public Function FileIsLocked(ByVal strFullFileName As String) As Boolean
Dim blnReturn As Boolean =
False
Dim fs As
System.IO.FileStream

Try
fs = System.IO.File.Open(strFullFileName, IO.FileMode.OpenOrCreate, IO.FileAccess.Read, IO.FileShare.None)
fs.Close()
Catch ex As
System.IO.IOException
blnReturn =
True
End Try

Return blnReturn
End Function

ctwalker at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 11

Thanks for the good idea (copying the file first)

If somebody knows a way to read a file that is already opened exclusively by an another process,

please tell me.

Silmon at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 12

Use FileShare.Read to read a file even if it is opened exclusively by an another process. The default value is FileShare.None, we need to change this.

Madheshwaran at 2007-9-3 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified