2012-05-23 73 views
4

我正在使用這個類寫入配置文件。代碼是builted和應用程序啓動,但每次我檢查app.config我沒有看到任何寫在裏面。我無法寫入配置文件

可能是什麼問題?

下面是代碼:

public class ConfigSettings 
    { 
     private ConfigSettings() { } 

     public static string ReadSetting(string key) 
     { 
      return ConfigurationManager.AppSettings[key]; 
     } 

     public static void WriteSetting(string key, string value) 
     { 
      // load config document for current assembly 
      XmlDocument doc = loadConfigDocument(); 

      // retrieve appSettings node 
      XmlNode node = doc.SelectSingleNode("//appSettings"); 

      if (node == null) 
       throw new InvalidOperationException("appSettings section not found in config file."); 

      try 
      { 
       // select the 'add' element that contains the key 
       XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key)); 

       if (elem != null) 
       { 
        // add value for key 
        elem.SetAttribute("value", value); 
       } 
       else 
       { 
        // key was not found so create the 'add' element 
        // and set it's key/value attributes 
        elem = doc.CreateElement("add"); 
        elem.SetAttribute("key", key); 
        elem.SetAttribute("value", value); 
        node.AppendChild(elem); 
       } 
       doc.Save(getConfigFilePath()); 
      } 
      catch 
      { 
       throw; 
      } 
     } 

     public static void RemoveSetting(string key) 
     { 
      // load config document for current assembly 
      XmlDocument doc = loadConfigDocument(); 

      // retrieve appSettings node 
      XmlNode node = doc.SelectSingleNode("//appSettings"); 

      try 
      { 
       if (node == null) 
        throw new InvalidOperationException("appSettings section not found in config file."); 
       else 
       { 
        // remove 'add' element with coresponding key 
        node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key='{0}']", key))); 
        doc.Save(getConfigFilePath()); 
       } 
      } 
      catch (NullReferenceException e) 
      { 
       throw new Exception(string.Format("The key {0} does not exist.", key), e); 
      } 
     } 

     private static XmlDocument loadConfigDocument() 
     { 
      XmlDocument doc = null; 
      try 
      { 
       doc = new XmlDocument(); 
       doc.Load(getConfigFilePath()); 
       return doc; 
      } 
      catch (System.IO.FileNotFoundException e) 
      { 
       throw new Exception("No configuration file found.", e); 
      } 
     } 

     private static string getConfigFilePath() 
     { 
      return AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString(); 
     } 
    } 

後,我用它來寫入文件:

ConfigSettings.WriteSetting("check", "true"); 
+0

是一個Windows應用程序? –

+0

是的,它是一個Windows應用程序 –

回答

3

檢查YourExeName.config文件代替的app.config

+0

你是什麼意思?我在我的項目 –

+0

中編寫了App.config文件編輯我的答案... – Adam

+1

OP寫入'app.config'在哪裏?代碼動態獲取文件名。 – Oded

4

你可以派生從System.Configuration.ConfigurationSection,並使其非常簡單:

public class MyConfigurationSection : System.Configuration.ConfigurationSection 
{ 
    [ConfigurationProperty("myProperty")] 
    public string MyProperty 
    { 
     get { return (string)this["myProperty"]; } 
     set { this["myProperty"] = value; } 
    } 
} 

然後你在你的app/web.config中添加你的configSection。

<configuration> 
    <configSections> 
     <section name="myConfiguration" type="MyConfiguration, MyAssembly" /> 
    </configSections> 

    <myConfiguration myProperty="someValue" /> 
</configuration> 

您可以隨時隨地獲取實例是這樣的:

ConfigurationManager.GetSection("myConfiguration") as MyConfiguration