0

I have an application that changes your connectionstring in a webconfig in your unauthorized exception. Is there a way I can allow the file to be accessed? Thanks for any help.

Exception Message: System.UnauthorizedAccessExeption: Access to the path "PATH" is denied. //File path to xml file

var adminConnectionStringConfig = new FileInfo(e.Data.Details.VSStudio.Solution.FullName).Directory
                                  + @"\Web.Admin\ConnectionStrings.config";

//Method that updates the xml file
private void SetupConnectionStrings(string path, string newDb)
{
    var doc = new XmlDocument();
    doc.Load(path);
    XmlNode node = doc.SelectSingleNode("configuration/connectionStrings");
    for (int i = 0; i < node.ChildNodes.Count; i++)
    {
        if (node.ChildNodes[i].Attributes["name"].Value == "SYS")
        {
            //Get ConnectionString for client project
            var connectionString = node.ChildNodes[i].Attributes["connectionString"].Value;
            // Cut all 
            var dbName = node.ChildNodes[i].Attributes["connectionString"]
                .Value.Substring(connectionString.IndexOf("Catalog="));
            dbName = dbName.Replace("Catalog=", "");
            //Db Name that we will now replace
            dbName = dbName.Substring(0, dbName.IndexOf(";"));
            // System.Diagnostics.Debugger.Launch();
            doc.InnerXml = doc.InnerXml.Replace(dbName, newDb);
            doc.Save(path);
            break;
        }
    }
}
joce
  • 9,624
  • 19
  • 56
  • 74
user516883
  • 8,868
  • 23
  • 72
  • 114

1 Answers1

1

There are some steps that you can take.

First, I'd make sure that your XML file is not read-only.

If that doesn't work, than you can run the project as an admin. there are some directories that windows doesn't let you modify if you're not an admin.

If you right click on visual studio, and select run as administrator, then It will automatically debug your project as an admin.