File Currently Used Or Not

R

rn5a

How do I find out whether a file is currently being used by some
process or not (including the ASP.NET process)?

A Web Form has a ListBox which lists all the directories & files
existing in a directory on the server. There are 2 Buttons as well -
'btnCreateFile' for creating a new file & 'btnRenameFile' for renaming
a file.

I am creating a new file by first checking whether the file name
entered by the user already exists or not. If not, then using
File.Create, the new file gets created else the user is shown a message
saying that the file already exists. Similarly for renaming files, I am
first checking whether the new file name the user has entered already
exists or not. If not, then using File.Move the file gets renamed.

I click 'btnCreateFile' which pops-up a JavaScript 'prompt' dialog. I
enter a file named, say, 'MyFile.txt' (without the quotes) & the new
file gets created. What I find is if I try to rename this newly created
file immediately after it gets created by clicking 'btnRename' (this
also pops-up a JavaScript 'prompt' dialog), then the following error
gets generated:

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

which points to the File.Move line which renames the file.

How do I overcome this error?

I know that the FileStream class supports asynchronous file access but
how do I rename a file (& also a directory) using the FileStream class?
 
L

Laurent Bugnion [MVP]

Hi,

How do I find out whether a file is currently being used by some
process or not (including the ASP.NET process)?

The way to do it is exception handling. Wrap your code in a Try / Catch
/ Finally. The Exception to catch in that case is (I think) a
IOException (it's easy to see the Exception type from the error message.
Make sure you catch the specific type, not just catch ( Exception ) ).

I click 'btnCreateFile' which pops-up a JavaScript 'prompt' dialog. I
enter a file named, say, 'MyFile.txt' (without the quotes) & the new
file gets created. What I find is if I try to rename this newly created
file immediately after it gets created by clicking 'btnRename' (this
also pops-up a JavaScript 'prompt' dialog), then the following error
gets generated:

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

which points to the File.Move line which renames the file.

How do I overcome this error?

File.Create opens the file for writing, so before you can do any other
operation on the file, you must Close it.

// Create the file.
FileStream fs = File.Create(path);
Byte[] info
= new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
fs.Close();

// When you're done working with the FileStream, dispose it
// to free the resources.
fs.Dispose();

// Now it's safe to move the file.


Alternatively, you can use a "using" statement in C#. This will
automatically close and dispose the file when you're done with it.

// Create the file.
using (FileStream fs = File.Create(path))
{
Byte[] info
= new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}

// Now it's safe to move the file.

I know that the FileStream class supports asynchronous file access but
how do I rename a file (& also a directory) using the FileStream class?

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top