C# 读取不在同一个程序集中的配置文件app.config
/// <summary> /// C#读取不在同一个程序集中的配置文件(代码没验证??) /// </summary> public static string ReportTemplateDirectory { get { string fileName =Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Mobot.TeamFoundation.Client.Win.exe.config"); StreamReader stream = new StreamReader(fileName); XmlDocument doc = new XmlDocument(); doc.Load(stream); XmlNode applicationSettingsNode =doc.FirstChild.NextSibling; applicationSettingsNode = applicationSettingsNode.FirstChild; while (applicationSettingsNode != null) { if(applicationSettingsNode.Name.Equals("applicationSettings")) { XmlNode settingsNode=applicationSettingsNode.FirstChild; if(settingsNode.Name.Equals("Mobot.TeamFoundation.Client.Properties.Settings")) { XmlNode settingNode = settingsNode.FirstChild; for (int i = 0; i < settingNode.Attributes.Count; i++) { if (settingNode.Attributes[i].Name.Equals("name")) { if(settingNode.Attributes[i].Value.Equals("ReportTemplatePath")) { XmlNode valueNode = settingNode.FirstChild; fileName = valueNode.Attributes[i].Value; return fileName; } } } } } } if (!Directory.Exists(fileName) && fileName != "") { Directory.CreateDirectory(fileName); } if (!Directory.Exists(fileName) || fileName == "") { string docPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); docPath = Path.Combine(docPath, "ReportTemplate"); if (!Directory.Exists(docPath)) { Directory.CreateDirectory(docPath); } //Settings.Default.ReportTemplatePath = docPath; //Settings.Default.Save(); fileName = docPath; } return fileName; } }
/// <summary> /// 修改本地配置SvnHookDataBase连接字符 CreateProjectInfo /// </summary> private void ChangeXML(string xmlPath, string svnHookDataBase) { StreamReader stream = new StreamReader(xmlPath); XmlDocument doc = new XmlDocument(); doc.Load(stream); XmlNode connectNode = doc.FirstChild.NextSibling; connectNode = connectNode.FirstChild; while (connectNode != null) { if (connectNode.Name.Equals("connectionStrings")) { XmlNode addNode = connectNode.FirstChild; if (addNode.Name.Equals("add")) { for (int j = 0; j < addNode.Attributes.Count; j++) { if (addNode.Attributes[j].Name.Equals("name")) { if (addNode.Attributes[j].Value.Equals("DataBaseString")) { SetConfig(doc, addNode, connectNode); addNode = connectNode.FirstChild; SetConfig(doc, addNode, connectNode, svnHookDataBase); } else if (addNode.Attributes[j].Value.Equals("SvnHookDataBase")) { SetConfig(doc, addNode, connectNode, svnHookDataBase); addNode = connectNode.FirstChild; SetConfig(doc, addNode, connectNode); } } } } break; } connectNode = connectNode.NextSibling; } stream.Close(); doc.Save(xmlPath); }
清清软件园 http://sillysoft.taobao.com