C# 我无法写入配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10718698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I can't write into config file
提问by user123_456
I am using this class for writing into config file. Code is builted and app starts, but every time I check app.config I don't see anything written inside.
我正在使用这个类来写入配置文件。代码已构建并应用程序启动,但每次我检查 app.config 时,我都看不到里面有任何内容。
What could be the problem?
可能是什么问题呢?
Here is the code:
这是代码:
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();
}
}
After that I use this to write into file:
之后我用它来写入文件:
ConfigSettings.WriteSetting("check", "true");
采纳答案by Adam
check YourExeName.config file instead of app.config
检查 YourExeName.config 文件而不是 app.config
回答by Ufuk Hac?o?ullar?
You could derive from System.Configuration.ConfigurationSection and make it really simple:
您可以从 System.Configuration.ConfigurationSection 派生并使其变得非常简单:
public class MyConfigurationSection : System.Configuration.ConfigurationSection
{
[ConfigurationProperty("myProperty")]
public string MyProperty
{
get { return (string)this["myProperty"]; }
set { this["myProperty"] = value; }
}
}
Then you add your configSection in your app/web.config.
然后在 app/web.config 中添加 configSection。
<configuration>
<configSections>
<section name="myConfiguration" type="MyConfigurationSection, MyAssembly" />
</configSections>
<myConfiguration myProperty="someValue" />
</configuration>
You can get the instance anywhere like this:
您可以像这样在任何地方获取实例:
ConfigurationManager.GetSection("myConfiguration") as MyConfigurationSection

