如何在安装期间修改app.config

编程入门 行业动态 更新时间:2024-10-25 18:27:39
本文介绍了如何在安装期间修改app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨专家, 我正在寻找一种使用.msi文件编辑app.config连接字符串的方法。因为它需要由其他人安装。我不希望其他人明确地进入文件并编辑服务器地址和db用户名和密码。 我想拥有一个漂亮的界面或.msi文件将是一个非常好的选择。有人可以指导我在这个问题上的起点。 非常感谢您的帮助。

Hi Experts, I am searching for a way to edit app.config connection strings using .msi file. Since it needs to be installed by other people. I dont want others to explicitly go into file and edit the server address and db username and password. I want to have a nice interface or an .msi file would be a very good option. Can someone guide me with a starting point on this issue. Your help is highly appreciated.

推荐答案

如果你能在运行时更改app.config文件,这里有一个片段。 If you are able to change the app.config file at run time here is a snippet to do so. //Usage WriteSettings("KeyInAppConfig", "NewValues"); //This will find the KeyInAppConfig setting in app.config and set the value of it to NewValues

private static XmlDocument loadConfigDocument() { XmlDocument doc = null; try { doc = new XmlDocument(); doc.Load(getConfigFilePath()); return doc; } catch (System.IO.FileNotFoundException e) { throw new Exception("No config filed found.", e); } } private static string getConfigFilePath() { return Assembly.GetExecutingAssembly().Location + ".config"; } public static void WriteSetting(string key, string value) { XmlDocument doc = loadConfigDocument(); XmlNode node = doc.SelectSingleNode("//appSettings"); if (node == null) throw new InvalidOperationException("appSettings section not found in config file."); try { XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key)); if (elem != null) { elem.SetAttribute("value", value); } else { elem = doc.CreateElement("add"); elem.SetAttribute("key", key); elem.SetAttribute("value", value); node.AppendChild(elem); } doc.Save(getConfigFilePath()); } catch { throw; } }

希望有所帮助

Hope it helps

更多推荐

如何在安装期间修改app.config

本文发布于:2023-11-08 13:31:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569505.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   app   config

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!