Reputation: 57
I'm developing a C# Windows Forms Application that is connected to a DB using a WebService. While saving a XML config file, it's throwing an UnauthorizedAccessException. Here's the function responsible for the XML file creation (it's inside a class called MyXmlHandler):
public void CreateConfigXML(string path)
{
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
XmlElement root = doc.CreateElement("configuration");
doc.AppendChild(root);
XmlElement epubfolder = doc.CreateElement("epubfolder");
epubfolder.InnerText = @"C:\";
XmlElement webservicelink = doc.CreateElement("webservicelink");
webservicelink.InnerText = "http://localhost:20707/Service1.svc";
root.AppendChild(epubfolder);
root.AppendChild(webservicelink);
doc.Save(path);
}
And here's where I'm using the function:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
...
string xmlPath = Application.StartupPath + @"\Config\Config.xml";
string xsdPath = Application.StartupPath + @"\Config\Config.xsd";
MyXmlHandler xmlHandler = new MyXmlHandler(xmlPath, xsdPath);
if (Directory.Exists(Application.StartupPath + @"\Config") && File.Exists(xmlPath) && File.Exists(xmlPath) && xmlHandler.ValidateXml())
{
Application.Run(form);
}
else
{
if (!Directory.Exists(Application.StartupPath + @"\Config"))
{
Directory.CreateDirectory(Application.StartupPath + @"\Config");
}
xmlHandler.CreateConfigXML(Application.StartupPath + @"\Config");
MessageBox.Show(xmlHandler.ValidateMessage);
}
}
}
It's throwing the exception at doc.Save(path)
Thanks in advance
Upvotes: 0
Views: 300
Reputation: 1804
You're passing a directory path to your CreateConfigXML
function. Try this: Application.StartupPath + @"\Config\config.xml"
Edit: Or rather CreateConfigXML(xmlPath)
Upvotes: 1