2013-04-15 55 views
0

我有一个应用程序在未经授权的异常情况下更改webconfig中的连接字符串。有没有办法让我可以访问文件?谢谢你的帮助。更改XML文件时收到未经授权的异常

异常消息:System.UnauthorizedAccessExeption:访问路径“路径”被拒绝。 //文件路径xml文件

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; 
     } 
    } 
} 
+0

什么是异常消息? – SLaks

+0

添加了外观 – user516883

+0

异常在哪里? –

回答

1

有一些步骤,你可以采取。

首先,我会确保您的XML文件不是只读的。

如果这不起作用,那么您可以将该项目作为管理员运行。如果你不是管理员,那么有些目录不允许你修改。

如果你右击visual studio,并选择run as administrator,那么它会自动作为管理员调试你的项目。