public static class ConfigManager { private static string configName="app.xml"; private static string XMLPath = ""; private static XmlDocument xmlIAUConfig; public static XmlDocument XMLIAUConfig { get { return xmlIAUConfig; } set { xmlIAUConfig = value; } } private static string platForm; private static string PlatForm { get { return Environment.OSVersion.Platform.ToString(); } } private static string currentPath; public static string CurrentPath { get { if (PlatForm.Equals("WinCE")) { currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase); } else if (PlatForm.Equals("Win32NT")) { currentPath = Directory.GetCurrentDirectory(); }
return currentPath; } } static ConfigManager() { xmlIAUConfig = new XmlDocument(); XMLPath = CurrentPath; Int32 i = XMLPath.LastIndexOf("/"); if (i >= 0) XMLPath = XMLPath.Remove(i); XMLPath = XMLPath + "//"+configName; xmlIAUConfig.Load(XMLPath); }
public static String GetValue(String key) { xmlIAUConfig.Load(XMLPath); String value; String path = @"//DataAccess/appSettings/add[@key='" + key + "']"; XmlNodeList xmlAdds = xmlIAUConfig.SelectNodes(path);
if (xmlAdds.Count == 1) { XmlElement xmlAdd = (XmlElement)xmlAdds[0];
value = xmlAdd.GetAttribute("value"); } else { throw new Exception("IAUConfig配置信息设置错误:键值为" + key + "的元素不等于1"); }
return value; } public static void SavaConfig(string strKey,string strValue) { XmlDocument XMLDoc = new XmlDocument(); XMLDoc.Load(configName); XmlNodeList list = XMLDoc.GetElementsByTagName("add");
for (int i = 0; i < list.Count; i++) { if (list[i].Attributes[0].Value == strKey) { list[i].Attributes[1].Value = strValue; } } StreamWriter swriter = new StreamWriter(configName); XmlTextWriter xw = new XmlTextWriter(swriter); xw.Formatting = Formatting.Indented; XMLDoc.WriteTo(xw); xw.Close(); swriter.Close(); } } (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|