本文介绍了如何在安装期间修改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
发布评论