Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a Xml file. While creating the Xml firstly I will check if the file already exists. If it exists I am trying to delete it. While deleting the existing file I will get error like:

"The process cannot access the file 'C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\XmlExercise\XmlExercise\bin\Debug\PurchaseBill.xml' "


Please Help.
Thanks in advance
Posted
Updated 17-Nov-10 19:26pm
v2

This error simply means that your application or any other application has already opened the file. You would want to check if you really closed all handles of this file before trying to delete it. If you would show us some code we might be able to give you a better advice.

Updated answer after comment:

You are trying to delete the file after you created it. You said you want to check if it already exists and then delete it. So, this is how it's done:
string path = Application.StartupPath + "\\PurchaseBill.xml";
if (File.Exists(path))
  File.Delete(path);

using(XmlWriter writer = XmlWriter.Create(path))
{
  writer = XmlWriter.Create(path); writer.WriteStartElement("Bill");   
  writer.WriteElementString("VoucherNo", dtHeaderInfo.Rows[0][0].ToString()); 
  writer.WriteElementString("VoucherType", dtHeaderInfo.Rows[0][1].ToString()); 
  writer.WriteElementString("VoucherDate", dtHeaderInfo.Rows[0][2].ToString()); 
  writer.WriteElementString("PartyId", dtHeaderInfo.Rows[0][3].ToString()); 
  writer.WriteElementString("BillNo", dtHeaderInfo.Rows[0][4].ToString());
  writer.WriteElementString("BillDate", dtHeaderInfo.Rows[0][5].ToString()); 
  writer.WriteEndElement(); 
  writer.Flush(); 
}
 
Share this answer
 
v2
Comments
Dalek Dave 18-Nov-10 4:47am    
Good Call.
Well, it's clearly security privilege issue. Make sure proper access securities are provided. Probably ASPNET needs to be given full permission to delete it.

You can start with giving Everyone as full permission to that file and check for the user that you are using in order to delete it.
 
Share this answer
 
I think the problem is simpler than the previous answer suggests. In the code example you posted, you have

write.Flush();


which flushes the buffers but does not close the file. You need to add a line to explicitly close the file

write.Close();


before trying to delete it.
 
Share this answer
 
Comments
Dalek Dave 18-Nov-10 4:47am    
Good answer.
JF2015 18-Nov-10 5:55am    
But would it make sense to create the file and then delete it again?
Geoff Williams 18-Nov-10 6:00am    
In the original code sample (which seems to have been removed), that is exactly what it was trying to do!

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