I'm writing a small class to manage configuration XML for an in-house application.
The following code is called each time a new list of t is loaded, and attempts to serialize data for new t, or, load where t already exists.
I'm missing something fundamental, but I'm not sure where.
The exception is thrown by this code
if (!File.Exists(Path.Combine(Environment.CurrentDirectory + "TaskData.xml")))
{
XDocument doc = new XDocument();
XElement rootElement = new XElement("ConfigData",
new XElement("Servers"),
new XElement("Paths"));
doc.Add(rootElement);
doc.Save("TaskData.xml");
}
The specific line is 'doc.Save("TaskData.xml")'.
On first iteration, this works fine, and the code creates the template file.
On second iteration, even when the xml file is in the directory, File.Exists returns 'false', and the doc.Save throws the permission exception.
Any help is greatly appreciated.
I'm surprised it's working on the first iteration - because you're not checking a useful file. Instead of calling Path.Combine
with multiple arguments, you're concatenating TaskData.xml
with the current directory. You want:
if (!File.Exists(Path.Combine(Environment.CurrentDirectory, "TaskData.xml")))
It's a pity that your current code even compiles... arguably the overloads for Path.Combine
should all have at least 2 required non-param-array parameters first...
See more on this question at Stackoverflow