Click here to Skip to main content
15,885,216 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 write the content to it but got the exception:

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


XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

XmlWriter writer = XmlWriter.Create("../../../connString.xml", settings);
writer.WriteStartDocument();
writer.WriteComment("This file is generated by the program.");
writer.WriteStartElement("ConnectionString");

if (ConnType == "SQL Server") {
   writer.WriteAttributeString("ID", "SQL Server");
   writer.WriteAttributeString("DataSource", Convert.ToString(TEServer.EditValue));
   writer.WriteAttributeString("Database", Convert.ToString(TEDatabase.EditValue));
   writer.WriteAttributeString("UserID", Convert.ToString(TEUserID.EditValue));
   writer.WriteAttributeString("Password", Convert.ToString(TEPassword.EditValue));
} else if (ConnType == "Access") {
   writer.WriteAttributeString("ID", "Access");
   writer.WriteAttributeString("DbLocation", Convert.ToString(BtnEditDBLoc.EditValue));
}

writer.WriteEndElement();
writer.WriteEndDocument();

writer.Flush();
writer.Close();


I've been looking at some sources said to be close "XmlWriter" but in my coding i have closed, with : writer.Close();

But still occurs the same error.

Is there anything that can help me, please?


Thanks.
Posted
Comments
[no name] 6-Jun-14 23:44pm    
This error occurs because the file is used by another process as the error says.

This is easy to investigate.

First of all, the similar question was asked here many times, and from this experience I know: in most cases the blocking process is your own process. You could have forgotten to dispose/close something in the same application. So, first of all, check it up. To explore this possibility, please see my past answer:
Clearing a Handle in C#[^].

In this answer, pay attention for the using of the using statement which helps you to guarantee that appropriate file system object is properly disposed after use, not keeping the file locked.

In same cases, you really need to investigate which process holds which file. For this, I recommend using one utility from the Sysinternals Suite. This set of utilities (formerly from Winternals company, presently at Microsoft) is a must-have for any developer, please see:
http://technet.microsoft.com/en-us/sysinternals/bb842062[^],
http://technet.microsoft.com/en-us/sysinternals/bb545027[^].

The utility you need is "handle.exe", please see:
http://technet.microsoft.com/en-us/sysinternals/bb896655[^].

In your case, you use it with file name parameter:
handle.exe <file_name>


This utility will scan all kinds of handles, not just file handles. For file, it will scan all file handles matching the file name (so it does not have to be a full path name) and return information sufficient to identify each process, including its pid. So, if you need more information on a process in question, you can also use other Sysinternals utilities, in particular, its Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653[^].

Good luck,
—SA
 
Share this answer
 
Comments
Peter Leow 7-Jun-14 0:28am    
Excellent answer. 5ed!
DamithSL 7-Jun-14 0:38am    
my 5 too :-)
Additional to SA's great answer you better use using block for XmlWriter, then it will close even you get exception in middle of your writing process. Note that using block is doing something equivalent to try catch and final block disposing..

C#
using (XmlWriter writer = XmlWriter.Create("employees.xml"))
{
   // do something with writer 
}
 
Share this answer
 

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