473,506 Members | 16,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File Currently Used Or Not

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?

Jan 16 '07 #1
1 1328
Hi,

rn**@rediffmail.com wrote:
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 ) ).

<snip>
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
--
Laurent Bugnion [MVP ASP.NET]
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Jan 17 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2047
by: sam | last post by:
I have to open a files for reading, which is currently being used by another process(say file X). I could open file X in notepad, without any issues. How can I open it in vb.net? I always get an...
1
2813
by: Peter Schmitz | last post by:
Hi, I'm just trying to play a *.wav file, making use of the unmanaged function "sndPlaySoundA" in winmm.dll, but I still got one question: How can I avoid, that the sound file itself has to be...
4
12431
by: hype | last post by:
Hi, How can I find out how much space has been used in the log file and how much of it is free or yet to be used ? Thanks, Hype
4
2076
by: bernden | last post by:
A C# program that I am currently working on contains about 100 textBoxes, 5 pictureBoxes, 20+ radioButtons ect. Concern / Question 1 (Save File) In the past when writing code to save the...
0
3910
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
6441
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
0
1954
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work...
5
1182
by: Andrew Cooper | last post by:
I posted this before but apparently it was eaten by the great Internet gods. I've got an application that watches a file. When that file is changed the application reads the data on the file...
5
6848
by: Nitin Mahajan | last post by:
Guys Is there a way in C# to create a word object directly from a memory stream without passing that to hard disk (file stream). I think it doesn't makes sense to create a file just to read it...
0
7218
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
7307
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
7370
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...
1
7021
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
7478
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
5614
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,...
1
5035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.