[CommandMethod("ChangeXml")]
public static void ChangeXml()
{
string filename = "../MineData.DLL";
string newName = "preUrl";
string newConString = "aaaa";
string newProviderName = "bbbb";
if (filename == null)
filename = GLBL.GetExecutingDllFullFileName();
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(filename);
if (config == null)
return;
bool exist = false; //记录该连接串是否已经存在
//如果要更改的连接串已经存在
if (config.ConnectionStrings.ConnectionStrings[newName] != null)
{
exist = true;
}
// 如果连接串已存在,首先删除它
if (exist)
{
config.ConnectionStrings.ConnectionStrings.Remove(newName);
}
//新建一个连接字符串实例
//ConnectionStringSettings mySettings =
// new ConnectionStringSettings(newName, newConString, newProviderName);
ConnectionStringSettings mySettings =
new ConnectionStringSettings(newName, newConString, newProviderName);
// 将新的连接串添加到配置文件中.
config.ConnectionStrings.ConnectionStrings.Add(mySettings);
// 保存对配置文件所作的更改
config.Save(ConfigurationSaveMode.Modified);
// 强制重新载入配置文件的ConnectionStrings配置节
ConfigurationManager.RefreshSection("connectionStrings");
}
[CommandMethod("ChangeXml")]
public static void ChangeXml()
{
string filename = "../MineData.DLL";
string newName = "preUrl";
string newConString = "aaaa";
string newProviderName = "bbbb";
if (filename == null)
filename = GLBL.GetExecutingDllFullFileName();
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(filename);
if (config == null)
return;
bool exist = false;
if (config.ConnectionStrings.ConnectionStrings[newName] != null)
{
exist = true;
}
if (exist)
{
config.ConnectionStrings.ConnectionStrings.Remove(newName);
}
//ConnectionStringSettings mySettings =
// new ConnectionStringSettings(newName, newConString, newProviderName);
ConnectionStringSettings mySettings =
new ConnectionStringSettings(newName, newConString, newProviderName);
config.ConnectionStrings.ConnectionStrings.Add(mySettings);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
}
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
namespace MineData
{
public class AppConfigFunction
{
/// <summary>
/// 依据连接串名字connectionName返回数据连接字符串
/// </summary>
///<param name="config">Configuration对象</param>
/// <param name="connectionName"></param>
/// <returns></returns>
public static string GetConnectionStringsConfig(Configuration config, string connectionName)
{
if (config == null)
return string.Empty;
string connectionString =
config.ConnectionStrings.ConnectionStrings[connectionName].ConnectionString.ToString();
return connectionString;
}
///<summary>
///更新连接字符串
///</summary>
///<param name="config">Configuration对象</param>
///<param name="newName">连接字符串名称</param>
///<param name="newConString">连接字符串内容</param>
///<param name="newProviderName">数据提供程序名称</param>
public static void UpdateConnectionStringsConfig(Configuration config, string newName, string newConString, string newProviderName)
{
if (config == null)
return;
bool exist = false; //记录该连接串是否已经存在
//如果要更改的连接串已经存在
if (config.ConnectionStrings.ConnectionStrings[newName] != null)
{
exist = true;
}
// 如果连接串已存在,首先删除它
if (exist)
{
config.ConnectionStrings.ConnectionStrings.Remove(newName);
}
//新建一个连接字符串实例
ConnectionStringSettings mySettings =
new ConnectionStringSettings(newName, newConString, newProviderName);
// 将新的连接串添加到配置文件中.
config.ConnectionStrings.ConnectionStrings.Add(mySettings);
// 保存对配置文件所作的更改
config.Save(ConfigurationSaveMode.Modified);
// 强制重新载入配置文件的ConnectionStrings配置节
ConfigurationManager.RefreshSection("connectionStrings");
}
///<summary>
///返回*.exe.config文件中appSettings配置节的value项
///</summary>
///<param name="config">Configuration对象</param>
///<param name="strKey"></param>
///<returns></returns>
public static string GetAppConfig(Configuration config, string strKey)
{
if (config == null)
return string.Empty;
foreach (string key in config.AppSettings.Settings.AllKeys)
{
if (key == strKey)
{
return config.AppSettings.Settings[strKey].Value.ToString();
}
}
return null;
}
///<summary>
///在*.exe.config文件中appSettings配置节增加一对键值对
///</summary>
///<param name="config">Configuration对象</param>
///<param name="newKey"></param>
///<param name="newValue"></param>
public static void UpdateAppConfig(Configuration config, string newKey, string newValue)
{
if (config == null)
return;
bool exist = false;
foreach (string key in config.AppSettings.Settings.AllKeys)
{
if (key == newKey)
{
exist = true;
}
}
if (exist)
{
config.AppSettings.Settings.Remove(newKey);
}
config.AppSettings.Settings.Add(newKey, newValue);
config.Save();
ConfigurationManager.RefreshSection("appSettings");
}
}
}
只在读的时候出现。无论是在app.config或是machine.config定义了两个key相同value不同的值时,都会出现(不论使用不使用):
相同key、value时,不会出现。
文章评论