多个应用项目使用共享配置方案!实例
2011-12-20 22:09
369 查看
项目一port是入口,启动项目,有一个app配置文件,demeAPPTwo是个第二个项目,这里为了模拟启动项目。Port用到了引用了demeAPPTwo。具体从port打开demeAPPTwo的窗体,demeAPPTwo的 窗体上调用自己项目内的AppConfigHelp类。获得全局的配置文件,当然路径时重点。
上代码:
/// /// ConfigClass 的摘要说明。
public class AppConfigHelp
{
public string strFileName;
public string configName;
public string configValue;
public AppConfigHelp()
{
// // TODO: 在此处添加构造函数逻辑 //
}
public string ReadConfig(string configKey)
{
configValue = "";
configValue = ConfigurationSettings.AppSettings["" + configKey + ""];
return configValue;
}
//得到程序的config文件的名称以及其所在的全路径
public void SetConfigName(string strConfigName)
{
configName = strConfigName; //获得配置文件的全路径
GetFullPath();
}
public void GetFullPath()
{ //获得配置文件的全路径
strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + configName;
}
public void SaveConfig(string configKey, string configValue)
{
XmlDocument doc = new XmlDocument();
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{ //获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == "" + configKey + "")
{ //对目标元素中的第二个属性赋值
att = nodes[i].Attributes["value"];
att.Value = configValue;
break;
}
} //保存上面的修改
doc.Save(strFileName);
}
/// <summary>
/// 读取Portal_.exe.config参数.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string GetDefinedEXEPathAppConfig(string key, string exe_config_path)
{
//System.Xml.XmlNode configNode = null;
if (string.IsNullOrEmpty(exe_config_path))
{
string configFileName = Application.StartupPath + "\\Portal.exe.config";//请根据实际情况修改
if (File.Exists(configFileName)==false)
{
return "";
}
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(configFileName);
//string configString = @"[@add='" + name + "']";//请根据实际情况修改
//configNode = doc.SelectSingleNode(configString);
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{ //获得将当前元素的key属性
if (nodes[i].Attributes[0].Name == "key") //有些不是add key的形式的
{
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == "" + key + "")
{ //对目标元素中的第二个属性赋值
att = nodes[i].Attributes["value"];
configValue = att.Value;
return configValue;
break;
}
}
}
}
else
{
string configFileName = Application.StartupPath + "\\" + exe_config_path;//请根据实际情况修改
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(configFileName);
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{ //获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (nodes[i].Attributes[0].Name == "key") //有些不是add key的形式的
{
if (att.Value == "" + key + "")
{ //对目标元素中的第二个属性赋值
att = nodes[i].Attributes["value"];
configValue = att.Value;
return configValue;
break;
}
}
}
}
return "";
}
}
}
下面是是很简单了 调用 一看就懂 不写了
上代码:
/// /// ConfigClass 的摘要说明。
public class AppConfigHelp
{
public string strFileName;
public string configName;
public string configValue;
public AppConfigHelp()
{
// // TODO: 在此处添加构造函数逻辑 //
}
public string ReadConfig(string configKey)
{
configValue = "";
configValue = ConfigurationSettings.AppSettings["" + configKey + ""];
return configValue;
}
//得到程序的config文件的名称以及其所在的全路径
public void SetConfigName(string strConfigName)
{
configName = strConfigName; //获得配置文件的全路径
GetFullPath();
}
public void GetFullPath()
{ //获得配置文件的全路径
strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + configName;
}
public void SaveConfig(string configKey, string configValue)
{
XmlDocument doc = new XmlDocument();
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{ //获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == "" + configKey + "")
{ //对目标元素中的第二个属性赋值
att = nodes[i].Attributes["value"];
att.Value = configValue;
break;
}
} //保存上面的修改
doc.Save(strFileName);
}
/// <summary>
/// 读取Portal_.exe.config参数.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string GetDefinedEXEPathAppConfig(string key, string exe_config_path)
{
//System.Xml.XmlNode configNode = null;
if (string.IsNullOrEmpty(exe_config_path))
{
string configFileName = Application.StartupPath + "\\Portal.exe.config";//请根据实际情况修改
if (File.Exists(configFileName)==false)
{
return "";
}
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(configFileName);
//string configString = @"[@add='" + name + "']";//请根据实际情况修改
//configNode = doc.SelectSingleNode(configString);
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{ //获得将当前元素的key属性
if (nodes[i].Attributes[0].Name == "key") //有些不是add key的形式的
{
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == "" + key + "")
{ //对目标元素中的第二个属性赋值
att = nodes[i].Attributes["value"];
configValue = att.Value;
return configValue;
break;
}
}
}
}
else
{
string configFileName = Application.StartupPath + "\\" + exe_config_path;//请根据实际情况修改
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(configFileName);
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{ //获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (nodes[i].Attributes[0].Name == "key") //有些不是add key的形式的
{
if (att.Value == "" + key + "")
{ //对目标元素中的第二个属性赋值
att = nodes[i].Attributes["value"];
configValue = att.Value;
return configValue;
break;
}
}
}
}
return "";
}
}
}
下面是是很简单了 调用 一看就懂 不写了
相关文章推荐
- 使用Log4j为项目配置日志输出应用详细总结及示例演示.
- Linux C编程--进程间通信(IPC)6--综合应用实例--信号量和共享内存的使用
- Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作
- Asp.Net Core 2.0 项目实战(6)Redis配置、封装帮助类RedisHelper及使用实例
- Tomcat配置多应用共享Session(使用crossContext)
- 使用Log4j为项目配置日志输出应用详细总结及示例演示.
- Web开发之tomcat配置及使用(环境变量设置及测试,一个简单的web应用实例)
- 使用Log4j为项目配置日志输出应用详细总结及示例演示.
- CentOS下使用SVN实现多项目管理配置方案
- ThinkPHP多应用/项目配置技巧(使用同一配置文件)--(十六)
- Quartz学习4:Quartz集群原理及配置应用,实例使用
- 使用Log4j为项目配置日志输出应用详细总结及示例演示.
- tomcat配置及使用(环境变量设置及测试,一个简单的web应用实例)
- Zabbix(五):高级应用-web方案、被动监控、基于snmp协议被动监控、proxy配置测试实例
- 使用手动配置的方式开发第一个Struts项目的步骤以及实例
- Linux C编程--进程间通信(IPC)6--综合应用实例--信号量和共享内存的使用
- Quartz集群原理及配置应用,实例使用
- 【转】解压缩版tomcat配置及使用(环境变量设置及测试,一个简单的web应用实例)
- CentOS6.5下使用SVN实现多项目管理配置方案
- ThinkPHP多应用/项目配置技巧(使用同一配置文件)--(十六)