无法写入配置文件我正在使用这个类来写入配置文件。代码构建和应用程序启动,但每次我检查app.config时,我什么都看不到。可能是什么问题呢?这是代码:}publicstaticvoidWriteSetting(stringkey,stringvalue){//加载当前程序集的配置文档XmlDocumentdoc=loadConfigDocument();//检索appSettings节点XmlNodenode=doc.SelectSingleNode("//appSettings");if(node==null)thrownewInvalidOperationException("appSettingssectionnotfoundinconfigfile.");try{//选择包含键XmlElement的'add'元素elem=(XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']",key));if(elem!=null){//为键添加值elem.SetAttribute("value",value);}else{//没有找到键,所以创建'add'元素//并设置它的键/值属性elem=doc.CreateElement("add");elem.SetAttribute("键",键);elem.SetAttribute("值",值);node.AppendChild(elem);}文档.保存(getConfigFilePath());}抓住{扔;}}publicstaticvoidRemoveSetting(stringkey){//加载当前程序集的配置文档XmlDocumentdoc=loadConfigDocument();//检索appSettings节点XmlNodenode=doc.SelectSingleNode("//appSettings");try{if(node==null)thrownewInvalidOperationException("appSettingssectionnotfoundinconfigfile.");else{//删除具有对应键的'add'元素node.RemoveChild(node.SelectSingleNode(string.Format("//add[@key='{0}']",key)));doc.Save(getConfigFilePath());}}catch(NullReferenceExceptione){thrownewException(string.Format("键{0}不存在。",key),e);}}privatestaticXmlDocumentloadConfigDocument(){XmlDocumentdoc=null;尝试{doc=newXmlDocument();doc.Load(getConfigFilePath());返回文档;}catch(System.IO.FileNotFoundExceptione){thrownewException("没有找到配置文件。",e);}}privatestaticstringgetConfigFilePath(){返回AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString();之后我用它来写入文件:ConfigSettings.WriteSetting("check","true");检查YourExeName.config文件而不是app.config您可以从System.Configuration.ConfigurationSection派生并使其非常简单:(字符串)这个[“我的财产”];}设置{这个["myProperty"]=值;}}}然后在app/web.config中添加configSection可以这样在任何地方获取实例:以上是C#学习教程:配置文件分享的全部内容我写不完,如果对你有用,你需要想了解更多C#学习教程,希望大家多加关注—ConfigurationManager.GetSection("myConfiguration")asMyConfigurationSection本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
