473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

"The process cannot access the file" when trying to write to filethe second time...

The first time I called this function, everything went well, but the
second time I called it. An Exception thrown, do you know why?

An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll

Additional information: The process cannot access the file
"E:\......\bin\Debug\test.xml" because it is being used by another process.

----------------------------------------------
public void updateXMLFile()
{
string AppPath = System.Windows.Forms.Application.StartupPath;
System.Xml.XmlTextWriter XMLWriter = new
System.Xml.XmlTextWriter(AppPath + "/test.xml", System.Text.Encoding.UTF8);

XMLWriter.Formatting = System.Xml.Formatting.Indented;
XMLWriter.Indentation = 1;

/// <summary>
/// <?xml version="1.0" encoding="UTF-8"?>
/// </summary>
XMLWriter.WriteStartDocument();

/// <summary>
/// <root>
/// </summary>
XMLWriter.WriteStartElement("root");

/// <summary>
/// </root>
/// </summary>
XMLWriter.WriteEndElement();

XMLWriter.WriteEndDocument();

XMLWriter.Flush();
XMLWriter.Close();
}
----------------------------------------------
Nov 16 '05 #1
2 3474
Jax
Are you accessing the file elsewhere
It sounds like your standard filestream left open exception. I used to get them all the time
Logging out and back in will fix it without having to restart. You want to use try, and finally to ensure you always close the connection
I can see that you are following the protocol in the example (by closing the XMLWriter) so i'd suggest looking elsewhere in the code, or checking the documentation on the XMLWriter object to see if you have to close it another way
I don't know much about the object myself so i'm afraid I cant be of any more use, if I have been of any. :

Hope that help

jax
Nov 16 '05 #2
Origin Problem:

The first time I called this function, everything went well, but the
second time I called it. An Exception thrown, do you know why?

An unhandled exception of type 'System.IO.IOException' occurred in
mscorlib.dll

Additional information: The process cannot access the file
"E:\......\bin\Debug\test.xml" because it is being used by another process.

----------------------------------------------
public void updateXMLFile()
{
string AppPath = System.Windows.Forms.Application.StartupPath;
System.Xml.XmlTextWriter XMLWriter = new
System.Xml.XmlTextWriter(AppPath + "/test.xml", System.Text.Encoding.UTF8);

XMLWriter.Formatting = System.Xml.Formatting.Indented;
XMLWriter.Indentation = 1;

/// <summary>
/// <?xml version="1.0" encoding="UTF-8"?>
/// </summary>
XMLWriter.WriteStartDocument();

/// <summary>
/// <root>
/// </summary>
XMLWriter.WriteStartElement("root");

/// <summary>
/// </root>
/// </summary>
XMLWriter.WriteEndElement();

XMLWriter.WriteEndDocument();

XMLWriter.Flush();
XMLWriter.Close();
}
----------------------------------------------
Jax wrote:
Are you accessing the file elsewhere?
It sounds like your standard filestream left open exception. I used to get them all the time.
Logging out and back in will fix it without having to restart. You want to use try, and finally to ensure you always close the connection.
I can see that you are following the protocol in the example (by closing the XMLWriter) so i'd suggest looking elsewhere in the code, or checking the documentation on the XMLWriter object to see if you have to close it another way.
I don't know much about the object myself so i'm afraid I cant be of any more use, if I have been of any. :)

Hope that helps

jax


I've solved the problem. I changed the class which this function belong
to to a singleton (and it should be a singleton), everything work as
expected.

BTW, I still don't know why the XmlTextWriter didn't close properly...
Nov 16 '05 #3

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

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
0
by: Sugapablo | last post by:
I have two ASP pages. They basically query a database, and spit out the information as plain text in CSV format. The first has the SQL query hardcoded into it. The second takes a SQL query...
2
by: Mark | last post by:
Hi, I'm running visual studio 2003, and am now receiving the error "The Operation Could Not Be Completed" when attempting to add a form / control or component class to a project. I can add any...
145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
2
by: FrzzMan | last post by:
The first time I called this function, everything went well, but the second time I called it. An Exception thrown, do you know why? An unhandled exception of type 'System.IO.IOException' occurred...
2
by: SammyBar | last post by:
Hi all, I'm trying to send a message from ASP.NET to another PC by using MSMQ. I created my ASP.NET project by using Visual Studio 2005 but I initially set the project to be located on the File...
9
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a...
2
by: Wimpie van Lingen | last post by:
Hey I have some more questions with regards to Remoting in .NET 2. I'm using TCP with the Binary formatter. My solution consists of 4 projects: - Class Library containing the server classes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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,...

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.