您的位置:首页 > 其它

多个应用项目使用共享配置方案!实例

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 "";

}

}

}

下面是是很简单了 调用 一看就懂 不写了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐