请问一下,VS2010中如何读取、修改WinForm里面App.config的配置值啊?(解决给分)

SXC585 2011-04-29 11:11:26
在VS2010(C#)中,如何读取、修改WinForm里面App.config配置的节点值呢??

配置如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DirectoryPath" value="" />
</appSettings>
</configuration>

如何读取、修改节点“DirectoryPath”的值呢??

解决给分!!!
...全文
1429 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Icedmilk 2011-04-29
  • 打赏
  • 举报
回复
如果你只希望读配置,可以这样:

Console.WriteLine(ConfigurationManager.AppSettings["DirectoryPath"]);


如果你希望修改并保存配置,可以这样

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Console.WriteLine(config.AppSettings.Settings["DirectoryPath"].Value);
config.AppSettings.Settings["DirectoryPath"].Value = "D:\\";
config.Save();
快乐大法师 2011-04-29
  • 打赏
  • 举报
回复
http://www.cnblogs.com/wl98766789/archive/2010/09/13/1825255.html
SXC585 2011-04-29
  • 打赏
  • 举报
回复
还有其他方法吗??

4.0里面有没有提供什么方法可以直接调用进行读取、修改的??
whzxl06018 2011-04-29
  • 打赏
  • 举报
回复
可以用用下面的方法,大致是可以设置配置文件中的值。
XmlDocument xd = new XmlDocument();
XmlElement xe;
//配置文件路径
string strPath = SystemConfig.Location + @"\config\Services-All.config";
try
{
//读取配置文件
xd.Load(strPath);
xe = (XmlElement)xd.SelectSingleNode("configuration/properties/DALassemblyname");
if (strvalue.Equals(Consts.Oraclevalue, StringComparison.InvariantCulture))
{
//设置配置文件信息
xe.InnerText = "OracleDAL";
}
else
{
//设置配置文件信息
xe.InnerText = "SQLServerDAL";
}
//保存配置文件
xd.Save(strPath);
}
catch (Exception ex)
{
throw ex;
}
cjh200102 2011-04-29
  • 打赏
  • 举报
回复
楼上不错

[Quote=引用 4 楼 icedmilk 的回复:]

如果你只希望读配置,可以这样:
C# code

Console.WriteLine(ConfigurationManager.AppSettings["DirectoryPath"]);



如果你希望修改并保存配置,可以这样
C# code

Configuration config = ConfigurationManager.……
[/Quote]

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧