我不能写入配置文件 [英] I can't write into config file
本文介绍了我不能写入配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用这个类写入配置文件。代码builted和应用程序启动,但每次我检查的app.config我没有看到任何东西里面写。
这可能是什么问题?
下面是代码:
公共类ConfigSettings
{
私人ConfigSettings(){}
公共静态字符串ReadSetting(字符串键)
{
返回ConfigurationManager.AppSettings [关键]
}
公共静态无效WriteSetting(字符串键,字符串值)当前装配
XmlDocument的DOC
{
//加载配置文件= loadConfigDocument( );
//检索的appSettings节点
XmlNode的节点= doc.SelectSingleNode(//的appSettings);
如果(节点== NULL)
抛出新的InvalidOperationException异常(的appSettings部分配置文件未找到。);
试
{
//选择包含关键
的XmlElement ELEM =(XmlElement的)node.SelectSingleNode(的String.format的添加元素(/ /添加[@key ='{0}]键));
如果(ELEM!= NULL)
{
//关键
elem.SetAttribute(价值,价值)增加价值;
}
,否则
{
//关键是没有找到这样创建的添加元素
//并设置它的键/值属性
ELEM = doc.CreateElement(加);
elem.SetAttribute(密钥,密钥);
elem.SetAttribute(价值,价值);
node.AppendChild(ELEM);
}
doc.Save(getConfigFilePath());
}
抓
{
扔;
}
}
公共静态无效RemoveSetting(字符串键)当前装配
XmlDocument的DOC
{
//加载配置文件= loadConfigDocument();
//检索的appSettings节点
XmlNode的节点= doc.SelectSingleNode(//的appSettings);
试
{
如果(节点== NULL)
抛出新的InvalidOperationException异常(在配置文件中未发现的appSettings部分。);
,否则
{
//删除'添加'元素与coresponding键
node.RemoveChild(node.SelectSingleNode(的String.Format(//添加[@key ='{ 0}]键)));
doc.Save(getConfigFilePath());
}
}
赶上(NullReferenceException异常E)
{
抛出新的异常(的String.Format({0}不存在的关键。键) ,E);
}
}
私有静态的XmlDocument loadConfigDocument()
{
XmlDocument的DOC = NULL;
试
{$ B $(DOC)B =新的XmlDocument();
doc.Load(getConfigFilePath());
返回文档;
}
赶上(System.IO.FileNotFoundException E)
{
抛出新的异常(无配置文件中找到。,E);
}
}
私人静态字符串getConfigFilePath()
{
返回AppDomain.CurrentDomain.GetData(APP_CONFIG_FILE)的ToString()。
}
}
之后,我使用它来写入文件:
ConfigSettings.WriteSetting(检查,真);
解决方案
检查YourExeName.config文件,而不是的app.config
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.
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");
解决方案
check YourExeName.config file instead of app.config
这篇关于我不能写入配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文