C#WinForm APP.CONFIG配置文件的操作
2010-06-03 16:33
741 查看
取值代码:首先添加应用,System.configuration
string serverName = System.Configuration.ConfigurationManager.AppSettings["serverName"];
string serverIp = System.Configuration.ConfigurationManager.AppSettings["serverIp"];
动态修改或者添加信息到配置文件中代码:
private void frmTransfer_FormClosing(object sender, FormClosingEventArgs e)
{
string jobSaveValue = txtSavePath.Text.Trim();
string tempLateValue = txtSaveTempLate.Text.Trim();
SaveConfig(jobSaveValue,tempLateValue);
}
private void SaveConfig(string jobSaveValue,string tempLateValue)
{
string jobSave = "jobSavePath";
string tempLate = "templateSavePath";
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
List<string> keys = new List<string>(config.AppSettings.Settings.AllKeys);
if (keys.Contains(jobSave))
{
config.AppSettings.Settings[jobSave].Value = jobSaveValue;
}
else
{
config.AppSettings.Settings.Add(jobSave , jobSaveValue);
}
if (keys.Contains(tempLate))
{
config.AppSettings.Settings[tempLate].Value = tempLateValue;
}
else
{
config.AppSettings.Settings.Add(tempLate,tempLateValue);
}
config.Save();
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
}
string serverName = System.Configuration.ConfigurationManager.AppSettings["serverName"];
string serverIp = System.Configuration.ConfigurationManager.AppSettings["serverIp"];
动态修改或者添加信息到配置文件中代码:
private void frmTransfer_FormClosing(object sender, FormClosingEventArgs e)
{
string jobSaveValue = txtSavePath.Text.Trim();
string tempLateValue = txtSaveTempLate.Text.Trim();
SaveConfig(jobSaveValue,tempLateValue);
}
private void SaveConfig(string jobSaveValue,string tempLateValue)
{
string jobSave = "jobSavePath";
string tempLate = "templateSavePath";
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
List<string> keys = new List<string>(config.AppSettings.Settings.AllKeys);
if (keys.Contains(jobSave))
{
config.AppSettings.Settings[jobSave].Value = jobSaveValue;
}
else
{
config.AppSettings.Settings.Add(jobSave , jobSaveValue);
}
if (keys.Contains(tempLate))
{
config.AppSettings.Settings[tempLate].Value = tempLateValue;
}
else
{
config.AppSettings.Settings.Add(tempLate,tempLateValue);
}
config.Save();
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
}
相关文章推荐
- c# winform app.config xml 配置文件 读写操作
- C#----操作应用程序配置文件App.config
- C#操作配置文件App.Config简介
- C# 操作配置文件 App.config
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
- ASP.NET(C#)应用程序配置文件app.config/web.config的增、删、改操作
- C# 操作App.config 配置文件帮助类
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
- C# WinForm程序App.Config数据库连接配置文件的使用过程
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
- C#操作配置文件app.config
- c# 配置文件App.config操作类库的方法
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
- C#_WinForm程序的App.Config数据库连接配置文件
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作
- C#_WinForm程序的App.Config数据库连接配置文件
- C# WinForm程序的App.Config数据库连接配置文件
- c#Winform程序调用app.config文件配置数据库连接字符串