Ok, this is what I gt for writing code so early and without full cup of coffee in me (not to mention to medication lol), but in one method I create a file => urlList.xml => like so:
CODE
private string BuildUrlFile()
{
//first build our variables and paths
string filePath = GetFolderPath();
//variable to hold he file and path
string returnValue;
try
{
//now check to see if the file exists
if (!(File.Exists(filePath + "\\" + _file)))
{
//file doesnt exists so we need to create it
File.Create(filePath + "\\" + _file);
}
returnValue = filePath + "\\" + _file;
}
catch (DirectoryNotFoundException ex)
{
MessageBox.Show(ex.Message);
returnValue= string.Empty;
}
catch (FileNotFoundException ee)
{
MessageBox.Show(ee.Message);
returnValue= string.Empty;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
returnValue = string.Empty;
}
return returnValue;
}
Which of course works fine, creates the document right where I want it. But when I try to write to this newly created document, the first time, when it is created, I get an exception telling me I cannot write to the file because it is being accesses by another resource.
The code I'm using to write to the XML file is as follows:
CODE
public void createUrlList()
{
try
{
//build out XMLTextWriter Object so we can add this url to the list
XmlTextWriter writer = new XmlTextWriter(BuildUrlFile(),null);
//set the XML formatting
writer.Formatting = Formatting.Indented;
//write the opening element of the XML doc
writer.WriteStartDocument(false);
//write document type
writer.WriteDocType("url",null,"url.dtd",null);
//write a comment, so all will know what this file is for
writer.WriteComment("This file will hold all the url's the user types into their address bar");
//now we write the start element
writer.WriteStartElement("urlList");
//now we start the document URL element
writer.WriteStartElement("URL",null);
//now we start writing our attributes of the URL
//element: Name, URL, Date
writer.WriteAttributeString("SiteName",_siteName);
writer.WriteAttributeString("SiteURL",_url);
writer.WriteAttributeString("DateAdded",_added.ToString());
//now we end the elements of the document
writer.WriteEndAttribute();
//close our 2 start elements
writer.WriteEndElement();
writer.WriteEndElement();
//now end the document
writer.WriteEndDocument();
//now we flush and close the writer to clear the buffer
//and any memory & resources being held by the writer
writer.Flush();
writer.Close();
}
catch(XmlException ex)
{
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Now no one has ever considered me the "XML Guru" at work now lol, so no laughing at the code. I know if I create a new document with a StreamWriter or the likes I have to close said object or it holds it open so nothing else can manipulate it, but I don't even remember running into this using
File.Create().
Now the 2nd time the application runs of course there are no problems accessing the file (there are however errors when trying to write to the XML document, guess I got my closing attributes or something outta whack or order or something, but it would be nice if someone could help me with the first issue