怎样读取config文件中的值,修改后可以写回去?急 没试过..ConfigurationSettings.AppSettings["Application Name"] = "xx";行吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 只能够不读取。目前我还没有看到.net自己提供的写config文件的类。当然,你可以把他当作一般的xml文件,进行修改操作。上面的代码已经演示了如何使用。 string appName = ConfigurationSettings.AppSettings["Application Name"];上面这句话怎么理解?["Application Name"]是从哪个config文件读出来的呀? string appName = ConfigurationSettings.AppSettings["Application Name"];上面的问法不对,我是说要到哪个文件中去找key为“Application Name”的value 值??而上面什么都没有说明 假设你在1.exe中使用了string appName = ConfigurationSettings.AppSettings["Application Name"];那么,你的config文件的就是1.exe.config 在程序里是不能修改web.config或app.exe.config的,但我记得看到过:在vs.net的安装部署项目里提供对修改web.config或app.exe.config的支持,具体内容我当时每看。你自己查查help之类的。 可以修改try public static bool appSettingsEdit(string WebConfigDirectory,string appSettingsAddkey,string keyvalue) { try { string path=WebConfigDirectory+"\\web.config"; XmlDocument xd=new XmlDocument(); xd.Load(path); //如果没有appSetting,则添加 if(xd.SelectNodes("//appSettings").Count==0) { xd.DocumentElement.AppendChild(xd.CreateElement("appSettings")); } //判断节点是否存在,如果存在则修改当前节点 bool addNode=true; foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add")) { if(xn1.Attributes["key"].Value==appSettingsAddkey) { addNode=false; xn1.Attributes["value"].Value=keyvalue;// xn1.ParentNode.RemoveChild(xn1); break; } } //当前节点不存在,则添加新节点 if(addNode) { //创建新节点 XmlNode xn2=xd.CreateElement("add"); //添加key XmlAttribute xa=xd.CreateAttribute("key"); xa.Value=appSettingsAddkey; xn2.Attributes.Append(xa); //添加value xa=xd.CreateAttribute("value"); xa.Value=keyvalue; xn2.Attributes.Append(xa); xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2); } //保存web.config xd.Save(path); return true; } catch { return false; } } 可以修改try public static bool appSettingsEdit(string WebConfigDirectory,string appSettingsAddkey,string keyvalue) { try { string path=WebConfigDirectory+"\\web.config"; XmlDocument xd=new XmlDocument(); xd.Load(path); //如果没有appSetting,则添加 if(xd.SelectNodes("//appSettings").Count==0) { xd.DocumentElement.AppendChild(xd.CreateElement("appSettings")); } //判断节点是否存在,如果存在则修改当前节点 bool addNode=true; foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add")) { if(xn1.Attributes["key"].Value==appSettingsAddkey) { addNode=false; xn1.Attributes["value"].Value=keyvalue;// xn1.ParentNode.RemoveChild(xn1); break; } } //当前节点不存在,则添加新节点 if(addNode) { //创建新节点 XmlNode xn2=xd.CreateElement("add"); //添加key XmlAttribute xa=xd.CreateAttribute("key"); xa.Value=appSettingsAddkey; xn2.Attributes.Append(xa); //添加value xa=xd.CreateAttribute("value"); xa.Value=keyvalue; xn2.Attributes.Append(xa); xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2); } //保存web.config xd.Save(path); return true; } catch { return false; } } 可以修改try public static bool appSettingsEdit(string WebConfigDirectory,string appSettingsAddkey,string keyvalue) { try { string path=WebConfigDirectory+"\\web.config"; XmlDocument xd=new XmlDocument(); xd.Load(path); //如果没有appSetting,则添加 if(xd.SelectNodes("//appSettings").Count==0) { xd.DocumentElement.AppendChild(xd.CreateElement("appSettings")); } //判断节点是否存在,如果存在则修改当前节点 bool addNode=true; foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add")) { if(xn1.Attributes["key"].Value==appSettingsAddkey) { addNode=false; xn1.Attributes["value"].Value=keyvalue;// xn1.ParentNode.RemoveChild(xn1); break; } } //当前节点不存在,则添加新节点 if(addNode) { //创建新节点 XmlNode xn2=xd.CreateElement("add"); //添加key XmlAttribute xa=xd.CreateAttribute("key"); xa.Value=appSettingsAddkey; xn2.Attributes.Append(xa); //添加value xa=xd.CreateAttribute("value"); xa.Value=keyvalue; xn2.Attributes.Append(xa); xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2); } //保存web.config xd.Save(path); return true; } catch { return false; } } 我记得程序在运行的时候,自己是不能修改自己的config配置文件的呀,也许是我记错了,不过,逻辑上将也不应该让修改呀,比如web.config,aps.net只要感知应用程序的web.config被修改了,就会在接下来的请求到来时自动重启该web应用程序。 如果上面是对的话(不会错吧),应用程序如何能修改自己的配置呢?请 xhan2000(popeye.net) 再解释一下好吗? 我同意xhan2000 的code, 因为.net 其实并不想让程序修改config, config 是为了让system administrator 在不用修改程序source code 和re-compile 的情况下改变程序的行为所以要改的话需要把它当一般的xml document 造DOM(或SAX)修改后再写回文件系统我觉得如果在config 里面加一条描述config file 本身路径的node 就更灵活了例如<add key="ConfigFilePath" value = "MyApp.exe.config" />然后先把文件名读进程序,string filename = System.Configuration.ConfigurationSettings.AppSettingsGet("ConfigFilePath");然后再 XmlDocument doc = new XmlDocument();doc.Load(filename);...再对它做processing csharpguy(csharpguy) 你也实际测试一下好吧,我在上个项目中也试图在程序修改配置文件,查了一些资料后,看到我上面说的那些内容,就没有深究而打消了自己在程序修改的主意,我想实实在弄清这个问题,麻烦测试后并解释一下,谢谢! 我试了一下,程序如下private void button1_Click(object sender, System.EventArgs e){string filename = System.Configuration.ConfigurationSettings.AppSettingsGet("ConfigFileName");XmlDocument config = new XmlDocument();FileStream fs = new FileStream(filename, FileMode.Open);config.Load(fs);fs.Flush();fs.Close();string xpath = "//add[@key='textBox1.Text']";XmlNode node = config.DocumentElement.SelectSingleNode(xpath);XmlNode valueAtt = node.Attributes.GetNamedItem("value");if(valueAtt!=null) valueAtt.Value = "new value";FileStream wfs = new FileStream(filename, FileMode.Truncate);config.Save(wfs);wfs.Flush();wfs.Close();}这是我的 writeConfig.exe 的config<?xml version="1.0" encoding="utf-8"?><configuration> <appSettings> <add key="textBox1.Text" value="my value" /> <add key="ConfigFileName" value="writeConfig.exe.config" /> </appSettings></configuration> 程序运行后config 里 my value 变成 new value 这个是实际项目中截取的代码吧,我没法copy下来测试。Web应用程序测试过吗? 那是我随便放个button 在一个form 上然后写的 click event handler是winform application 的没有试过web config... 不能直接用System.Configuration.ConfigurationSettings.AppSettings["ConfigFileName"]=xxx来进行修改,这个我试过。用XML来修改我倒没试过,学习。 我改了这句,测试通过:string filename = System.Configuration.ConfigurationSettings.AppSettings["ConfigFileName"];现在关心Web.config是否也可这样修改,我继续测试看看。 web应用也测试了,程序运行没问题,但修改不了!好,搞清楚了。有收获,谢谢 我试了 web config, 也行,但要在 web.config 里加<identity impersonate="true" />当然还要把 Url 转换成 local pathjjcccc(): 我上面的code 只是一个button 的click handler,你要试的话,要把method 里面的内容copy 到你自己的event handler 里 我试过winform application 的代码string filename = System.Configuration.ConfigurationSettings.AppSettings.Get("ConfigFileName");这句话返回值为null,造成下面语句出错FileStream fs = new FileStream(filename, FileMode.Open);提示“路径不能为空”,应如何改,请指教 上面已经都说了:string filename = System.Configuration.ConfigurationSettings.AppSettings.Get("ConfigFileName");==》string filename = System.Configuration.ConfigurationSettings.AppSettings["ConfigFileName"]; 这句不是问题所在,问题是在config file 里你要自己加<appSettings><add key="ConfigFileName" value="app.exe.config" /><appSettings/>这样 AppSettings.Get 才能找到 "app.exe.config" 这个值,再给filename 变量 app.exe.config 应该是你的application 的config 文件名 项目要用Release配置编译,且app.exe.config 要放到bin\release目录下。 关于dataset和datatable 如何让Winform的TextBox失去焦点? 断线后自动重新连接 用.net的tcpclient类进行文件传输,如何保证不丢包以及速度控制? 想要做个随机抽取的小软件,希望高手指导 FtpClient组件更新, 顺祝五一快乐! 自定义控件中,IPAddress类型的自定义依赖属性 XmlDocument不可以Load一个Unicode的Xml文档? 如何将项目中包含的.dll文件安装到指定的文件夹?而不是应用程序文件夹. 诚交创业朋友 在C#中用WIN32API调用WINDOWS消息失败 累计列, 急!!
上面这句话怎么理解?["Application Name"]是从哪个config文件读出来的呀?
string appName = ConfigurationSettings.AppSettings["Application Name"];
那么,你的config文件的就是1.exe.config
{
try
{
string path=WebConfigDirectory+"\\web.config";
XmlDocument xd=new XmlDocument();
xd.Load(path); //如果没有appSetting,则添加
if(xd.SelectNodes("//appSettings").Count==0)
{
xd.DocumentElement.AppendChild(xd.CreateElement("appSettings"));
} //判断节点是否存在,如果存在则修改当前节点
bool addNode=true;
foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add"))
{
if(xn1.Attributes["key"].Value==appSettingsAddkey)
{
addNode=false;
xn1.Attributes["value"].Value=keyvalue;
// xn1.ParentNode.RemoveChild(xn1);
break;
}
} //当前节点不存在,则添加新节点
if(addNode)
{
//创建新节点
XmlNode xn2=xd.CreateElement("add"); //添加key
XmlAttribute xa=xd.CreateAttribute("key");
xa.Value=appSettingsAddkey;
xn2.Attributes.Append(xa); //添加value
xa=xd.CreateAttribute("value");
xa.Value=keyvalue;
xn2.Attributes.Append(xa);
xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2);
}
//保存web.config
xd.Save(path);
return true;
}
catch
{
return false;
}
}
{
try
{
string path=WebConfigDirectory+"\\web.config";
XmlDocument xd=new XmlDocument();
xd.Load(path); //如果没有appSetting,则添加
if(xd.SelectNodes("//appSettings").Count==0)
{
xd.DocumentElement.AppendChild(xd.CreateElement("appSettings"));
} //判断节点是否存在,如果存在则修改当前节点
bool addNode=true;
foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add"))
{
if(xn1.Attributes["key"].Value==appSettingsAddkey)
{
addNode=false;
xn1.Attributes["value"].Value=keyvalue;
// xn1.ParentNode.RemoveChild(xn1);
break;
}
} //当前节点不存在,则添加新节点
if(addNode)
{
//创建新节点
XmlNode xn2=xd.CreateElement("add"); //添加key
XmlAttribute xa=xd.CreateAttribute("key");
xa.Value=appSettingsAddkey;
xn2.Attributes.Append(xa); //添加value
xa=xd.CreateAttribute("value");
xa.Value=keyvalue;
xn2.Attributes.Append(xa);
xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2);
}
//保存web.config
xd.Save(path);
return true;
}
catch
{
return false;
}
}
{
try
{
string path=WebConfigDirectory+"\\web.config";
XmlDocument xd=new XmlDocument();
xd.Load(path); //如果没有appSetting,则添加
if(xd.SelectNodes("//appSettings").Count==0)
{
xd.DocumentElement.AppendChild(xd.CreateElement("appSettings"));
} //判断节点是否存在,如果存在则修改当前节点
bool addNode=true;
foreach(XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add"))
{
if(xn1.Attributes["key"].Value==appSettingsAddkey)
{
addNode=false;
xn1.Attributes["value"].Value=keyvalue;
// xn1.ParentNode.RemoveChild(xn1);
break;
}
} //当前节点不存在,则添加新节点
if(addNode)
{
//创建新节点
XmlNode xn2=xd.CreateElement("add"); //添加key
XmlAttribute xa=xd.CreateAttribute("key");
xa.Value=appSettingsAddkey;
xn2.Attributes.Append(xa); //添加value
xa=xd.CreateAttribute("value");
xa.Value=keyvalue;
xn2.Attributes.Append(xa);
xd.SelectSingleNode("/configuration/appSettings").AppendChild(xn2);
}
//保存web.config
xd.Save(path);
return true;
}
catch
{
return false;
}
}
如果上面是对的话(不会错吧),应用程序如何能修改自己的配置呢?
请 xhan2000(popeye.net) 再解释一下好吗?
例如
<add key="ConfigFilePath" value = "MyApp.exe.config" />
然后先把文件名读进程序,
string filename = System.Configuration.ConfigurationSettings.AppSettingsGet("ConfigFilePath");然后再
XmlDocument doc = new XmlDocument();
doc.Load(filename);
...
再对它做processing
private void button1_Click(object sender, System.EventArgs e)
{
string filename = System.Configuration.ConfigurationSettings.AppSettingsGet("ConfigFileName");XmlDocument config = new XmlDocument();
FileStream fs = new FileStream(filename, FileMode.Open);config.Load(fs);
fs.Flush();
fs.Close();string xpath = "//add[@key='textBox1.Text']";
XmlNode node = config.DocumentElement.SelectSingleNode(xpath);
XmlNode valueAtt = node.Attributes.GetNamedItem("value");
if(valueAtt!=null)
valueAtt.Value = "new value";FileStream wfs = new FileStream(filename, FileMode.Truncate);
config.Save(wfs);
wfs.Flush();
wfs.Close();
}这是我的 writeConfig.exe 的config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="textBox1.Text" value="my value" />
<add key="ConfigFileName" value="writeConfig.exe.config" />
</appSettings>
</configuration>
是winform application 的
没有试过web config...
string filename = System.Configuration.ConfigurationSettings.AppSettings["ConfigFileName"];现在关心Web.config是否也可这样修改,我继续测试看看。
<identity impersonate="true" />当然还要把 Url 转换成 local pathjjcccc(): 我上面的code 只是一个button 的click handler,你要试的话,要把method 里面的内容copy 到你自己的event handler 里
string filename = System.Configuration.ConfigurationSettings.AppSettings.Get("ConfigFileName");这句话返回值为null,
造成下面语句出错
FileStream fs = new FileStream(filename, FileMode.Open);
提示“路径不能为空”,应如何改,请指教
==》
string filename = System.Configuration.ConfigurationSettings.AppSettings["ConfigFileName"];
问题是在config file 里
你要自己加
<appSettings>
<add key="ConfigFileName" value="app.exe.config" />
<appSettings/>这样 AppSettings.Get 才能找到 "app.exe.config" 这个值,再给filename 变量