Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to update a file. I opened the file and then tried to copy another file content to it but got the exception:

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



Is there any solution to this.
Posted

Depends on how you are doing it, but the best guess (i.e. the usual mistake) is that when you open the file, you don't Dispose of all the elements that are involved. If you open the file with a stream in one method, and allow the stream to go out of scope at the end, then the file is still open and locked until the Garbage Collector comes along and Disposes of it for you. this may not happen for a very long time! Until it does happen, all other accesses will get "the fvile is in use by another process" because it is - windows does not know you have finished with it until the stream is Disposed.

Change the way you are accessing the file to either manually call the Dispose method, or enclose the code in a using block.

Sorry I can't be more specific, but without relevant code fragments, it is impossible!
 
Share this answer
 
Comments
labshasanbd 19-Dec-11 7:04am    
Used .Close and also .Dispose. But no good result. What i am doing is
fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
then
xmlDocument.Load(fs);
then
after few changes in xmlDocument and it's attributes and nodes i am creating another filestream like:
fstemp = new FileStream(pathtemp, FileMode.Create);
xmlDocument.Save(fstemp);
then
fstemp.Close();
fstemp.Dispose();
fstemp = null;

fs.Close();
fs.Dispose();
fs = null;

try
{
File.Copy(pathtemp, path, true);
}
catch (Exception ex)
{

}
Here Copy is generating the error::::)
OriginalGriff 19-Dec-11 7:24am    
Try an experiment: Remove the xmlDocument.Save and replace it with a manual text write to the stream. Leave all the other code including the Close and Dispose in place.
If this does not produce the error (and I suspect it won't) then it is possible that the XmlDocument.Save is an asynchronous write: i.e. it kicks off another thread to do the write and it may not be complete when the method call returns. Since XmlDocument is an extension to the DOM model, it is possible that this is occurring, but I can't see anything in the documentation about this.
If so, then the easiest way round this would be to rename the input file to a backup before reading it, then save the XML directly to the original name. You can then delete the backup (or leave it in place for safety).
Hi,


The problem is if you have deployed the file in IIS and then modfiying the file using an IDE it might not allow you to overwrite because the file is beign opened as the IIS turn on and off with the system automatically .So you want to modify your file go IIS and stop the IIS engine and modify your file.
I have used this to solve my problem hope if you are finding the same problem the could help out from this issue.

Regards
Sushanth K
 
Share this answer
 
The reason of error is bit simple. you have open a file but not properly close it. the instance remain live in memory.
write .Close() and .Dispose() method to release memory.
GC.close() will work for you after dispose()
 
Share this answer
 
Comments
labshasanbd 19-Dec-11 7:04am    
Used .Close and also .Dispose. But no good result. What i am doing is
fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
then
xmlDocument.Load(fs);
then
after few changes in xmlDocument and it's attributes and nodes i am creating another filestream like:
fstemp = new FileStream(pathtemp, FileMode.Create);
xmlDocument.Save(fstemp);
then
fstemp.Close();
fstemp.Dispose();
fstemp = null;

fs.Close();
fs.Dispose();
fs = null;

try
{
File.Copy(pathtemp, path, true);
}
catch (Exception ex)
{

}
Here Copy is generating the error::::)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900