如何设置和访问app.config文件啊
解决方案 »
- C#不能删除数据。。怎么回事。。。
- C#WinFrom窗体 DataGridView显示不出数据库数据
- 高人看一下c#的动态linkbutton分组循环这怎么做
- 如何达到tabControl效果
- 都一个星期了还没有人能解决 ??? 大家都没碰到过吗??? 非常谢谢高手解决 ,分不够再加!!!
- 怎样在.NET环境下编写COM组件
- cookies问题。asp些的cookies会在本地建立一个文件,asp.net没有在本地建立文件。
- 在安装的过程中我想修改注册表,请问打包的时候把修改注册表的代码放在那里,才能实现?
- (会者不难,难者不会)在Excel的COM组件中单元格对应的公式怎么取得。
- 请教各位:我在使用IXMLHTTPRequest的send方法时遇到麻烦!
- 不连接数据库如何用DataSet的ReadXML操作保存数据?(100分)
- 简单问题:一个WebBrowser刷新的时候会触发什么事件?
你的项目生成文件名(包含后缀名)+.config
例如:a.exe.config
引入命名空间
using System.Xml;
using System.Configuration;
然后声明ConfigXmlDocument _configDoc,就可以像访问xml文档一样访问了
{
private System.Xml.XmlDocument xmlDoc;
private string fileName;
private IDataSecurity iDataSecurity;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="filename">配置文件名字</param>
/// <param name="ids">加密解密类</param>
public WebConfig(string filename,IDataSecurity ids)
{
xmlDoc=new XmlDocument();
fileName=filename;
iDataSecurity=ids;
try
{
xmlDoc.Load(filename);
}
catch
{
xmlDoc=null;
}
}
/// <summary>
/// 获取相应的配置
/// </summary>
/// <param name="configField">配置字段</param>
/// <returns>如果取到该值返回对应值,没有则返回null</returns>
public string GetConfig(string configField)
{
// TODO: 添加 WebConfig.GetConfig 实现
XmlNode xmlNode=xmlDoc.SelectSingleNode("configuration/appSettings/add[@key='" +configField +"']");
if(xmlNode==null)
{
return null;
}
return iDataSecurity.Decrypt(xmlNode.Attributes["value"].Value);
} /// <summary>
/// 设置配置字段
/// </summary>
/// <param name="configField">配置字段</param>
/// <param name="configValue">配置值</param>
public void SetConfig(string configField, string configValue)
{
XmlElement xmlNode=(XmlElement)xmlDoc.SelectSingleNode("configuration/appSettings/add[@key='" +configField +"']");
if(xmlNode==null)
return;
xmlNode.SetAttribute("value",iDataSecurity.Encrypt(configValue));
xmlDoc.Save(fileName);
}
}
//app.config文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="connectionstring" value="User ID=sa;Data Source=.;Password=;Initial Catalog=test;Provider=SQLOLEDB.1;" />
<add key="TemplatePATH" value="Template" />
</appSettings>
</configuration>//app.config访问string _connectionString=ConfigurationSettings.AppSettings["connectionstring"];
2.自定义配置节//app.config文件<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Test1" type="System.Configuration.SingleTagSectionHandler"/>
<section name="Test2" type="System.Configuration.DictionarySectionHandler"/>
<section name="Test3" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<Test1 setting1="Hello" setting2="World"/>
<Test2>
<add key="Hello" value="World" />
</Test2>
<Test3>
<add key="Hello" value="World" />
</Test3>
</configuration>//app.config访问
//访问配置节Test1
IDictionary IDTest1 = (IDictionary)ConfigurationSettings.GetConfig("Test1");
string str = (string)IDTest1["setting1"] +" "+(string)IDTest1["setting2"];
MessageBox.Show(str); //输出Hello World //访问配置节Test1的方法2
string[] values1=new string[IDTest1.Count];
IDTest1.Values.CopyTo(values1,0);
MessageBox.Show(values1[0]+" "+values1[1]); //输出Hello World
//访问配置节Test2
IDictionary IDTest2 = (IDictionary)ConfigurationSettings.GetConfig("Test2");
string[] keys=new string[IDTest2.Keys.Count];
string[] values=new string[IDTest2.Keys.Count];
IDTest2.Keys.CopyTo(keys,0);
IDTest2.Values.CopyTo(values,0);
MessageBox.Show(keys[0]+" "+values[0]);
//访问配置节Test3
NameValueCollection nc=(NameValueCollection)ConfigurationSettings.GetConfig("Test3");
MessageBox.Show(nc.AllKeys[0].ToString()+" "+nc["Hello"]); //输出
{
string returnValue="";
try
{
returnValue=System.Configuration .ConfigurationSettings .AppSettings[key];
}
catch(Exception e)
{
MessageBox.Show (e.Message .ToString ());
}
return returnValue;
}
public static void WriteXml(string key,string key_value)
{
XmlDocument dc=new XmlDocument ();
dc.Load (filePath);
XmlNode node=dc.SelectSingleNode ("//appSettings");
if(node==null)
{
throw new InvalidOperationException ("Can't find \"appSettings\" Keys in the config file.");
}
try
{
XmlElement elem=(XmlElement)node.SelectSingleNode (string.Format ("//add[@key='{0}']",key));
if(elem!=null)
{
elem.SetAttribute ("value",key_value);
}
else
{
elem=dc.CreateElement ("add");
elem.SetAttribute ("key",key);
elem.SetAttribute ("value",key_value);
node.AppendChild (elem);
}
dc.Save (filePath);
}
catch
{
throw;
}
}