2016-07-19 46 views

回答

0

循環遍歷應用程序文件夾中的所有exe.config文件並使用XmlDocument;我正在更改所有連接字符串並再次保存。

string curAssembly = Assembly.GetExecutingAssembly().Location; 
      string FolderPath = Path.GetDirectoryName(curAssembly); 
      string[] files = Directory.GetFiles(FolderPath).Where(x => x.EndsWith(".config")).ToArray(); 
      foreach (string item in files) 
      { 
       XmlDocument XmlDoc = new XmlDocument(); 
       XmlDoc.Load(item); 
       foreach (XmlElement xElement in XmlDoc.DocumentElement) 
       { 
        if (xElement.Name == "connectionStrings") 
        { 
         foreach (XmlElement xChild in xElement) 
         { 
          if (xChild.Attributes.Count > 1 && xChild.Attributes[0].Value == ConfigSectionName) 
          { 
           xChild.Attributes[1].Value = "Data Source=" + cmbDatasource.Text + ";Initial Catalog=" + cmbDatabaseName.Text + ";UID=" + txtUserName.Text + ";password=" + txtPassword.Text + ";Integrated Security = false;"; 
           Connectionstring = xChild.Attributes[1].Value; 
          } 
         } 
        } 
       } 
       XmlDoc.Save(item); 
      } 
相關問題