从自定义安装程序类操作更新 .config文件
我尝试在安装过程中更新我的应用程序的.config文件( 通过.NET安装程序类操作 )。 但是,我似乎无法让ConfigurationManager列出任何属性或能够设置任何属性。
我从几个stackoverflowpost中了解了这种方法,这些post向我指出了本指南: http : //raquila.com/software/configure-app-config-application-settings-during-msi-install/
我已经调查了我的项目和他之间的差异,并注意到我的配置文件格式不同。 我相信这与我正在使用“设置”文件的事实有关。
在指南中格式化的配置文件如下所示:
我的地方看起来像:
asdfasdfasdf False True
为了解释发生了什么……我尝试打印出如下设置列表:
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath); // Try getting the Settings1 Section AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("Settings1"); // Also tried myNamespace.Settings1 if (appSettings != null) { valList = "Settings1: "; foreach (string key in appSettings.Settings.AllKeys) { string value = appSettings.Settings[key].Value; valList += ("Key: '" + key + "' = '" + value + "'\n"); } } else { valList = "appSettings was null"; } MessageBox.Show(valList); MessageBox.Show(valList);
我尝试了几种这种排列……在所有情况下输出都是“appSettings为null”。
我也试过用几种不同的方式初始化配置管理器……
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath); MessageBox.Show("Section Count: " + config.Sections.Count); MessageBox.Show("Has File: " + config.HasFile); MessageBox.Show("Namespace Declared: " + config.NamespaceDeclared); config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); MessageBox.Show("Section Count: " + config.Sections.Count); MessageBox.Show("Has File: " + config.HasFile); MessageBox.Show("Namespace Declared: " + config.NamespaceDeclared);
对于他们每个人,返回的部分计数是20.(我不知道20来自哪里……我原以为它是3)。
HasFile对于第一种情况是正确的而对于第二种情况则是错误的。
在两种情况下,Namespace声明都是false。
谢谢!
编辑(6-18-09):仍在调查这个问题。 别人有什么想法吗? 谢谢。
搜索关键字:“对象引用未设置为未设置为实例”< – 尝试写入属性时会发生这种情况。
我遇到了同样的问题,经过深入调查后,我发现了更新配置文件的任何部分(例如app.config)的最简单方法,即使用XPath。 我们有一个连接到Web服务的应用程序,在安装过程中,用户输入Web服务的URL,这应该保存在以下app.config文件中:
whatever comes from setup should go here
以下是在安装程序类中执行此操作的代码:
public override void Install(System.Collections.IDictionary stateSaver) { base.Install(stateSaver); string targetDirectory = Context.Parameters["targetdir"]; string param1 = Context.Parameters["param1"]; string path = System.IO.Path.Combine(targetDirectory, "app.config"); System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument(); xDoc.Load(path); System.Xml.XmlNode node = xDoc.SelectSingleNode("/configuration/applicationSettings/Intellisense.ApplicationServer.Properties.Settings/setting[@name='ApplicationServer_ApplicationServerUrl']/value"); node.InnerText = (param1.EndsWith("/") ? param1 : param1 + "/"); xDoc.Save(path); // saves the web.config file }
基本上,由于配置文件是基于XML的文档,我使用XPath表达式来定位特定节点并更改其值。
要尝试的一件事是将其从安装转移到提交以确保在尝试访问文件之前先写入文件。 或者,您可以使用自定义操作编写自己的文件,只需将配置文件更改为指向备用xml文件即可。
我参与了一个Sharepoint产品安装,在这里我挖掘了所有这些,我承认正常工作非常繁琐。 我基于安装参数动态创建配置和批处理文件。
您可以使用System.Configuration
命名空间访问这些设置,但是,它并不像我想的那么简单,回想起来使用System.Xml.Linq
要简单得多。 无论如何,这是我如何让它工作。
重要的概念是, applicationSettings
部分不是AppSettings
,它是ClientSettingsSection
类型支持的单独部分。
//Open the application level config file ExeConfigurationFileMap exeMap = new ExeConfigurationFileMap(); exeMap.ExeConfigFilename = String.Format("{0}.config", Context.Parameters["assemblypath"]); Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeMap, ConfigurationUserLevel.None); //Get the settings section ClientSettingsSection settingsSection = config.GetSectionGroup("applicationSettings").Sections .OfType().Single(); //Update "TheSetting" //I couldn't get the changes to persist unless //I removed then readded the element. SettingElement oldElement = settingsSection.Get("TheSetting"); settingsSection.Settings.Remove(oldElement); SettingElement newElement = new SettingElement("TheSetting", SettingSerializeAs.String); newElement.Value = new SettingValueElement(); newElement.Value.ValueXml = oldElement.Value.ValueXml.CloneNode(true); newElement.Value.ValueXml.InnerText = "Some New Value"; settingsSection.Add(newElement); //Save the changes config.Save(ConfigurationSaveMode.Full);
所以,正如你所看到的,简单。 :-S
您可以通过Settings.Default访问它,而不是通过ConfigurationManager访问您的设置配置。 设置更像是Visual Studiofunction而不是.NETfunction……这种便利性使您可以轻松地直观地设计应用程序配置,而不是手动将其写入appSettings或创建自定义配置部分。 但是,使用“设置”时呈现的配置架构是非标准的,并且可能难以手动访问。
Visual Studio应该在构建应用程序时为您生成一个Settings类,并且您应该能够通过Properties.Settings.Default访问该类。 它应该具有每个设置的属性,在您的情况下,以下内容:
Properties.Settings.Default.TESTSETTING Properties.Settings.Default.VerboseErrorMode Properties.Settings.Default.RunOnStartup
您应该能够读取和写入这些设置。 需要注意的一件重要事情……标记为“用户”设置的任何内容都不会写回{yourapplication} .exe.config文件…它将被写入用户隔离的配置文件存储中的User.config文件区域。 这是在XP上的C:\ Documents and Settings {username}和Vista上的C:\ Users {username}下的AppData文件夹中。 根据操作系统和用户配置文件,AppData下的子文件夹可能会更改,但它完全唯一,并且可以键入特定版本的应用程序。 安装较新版本将导致在同一个键控文件夹下存储一组全新的配置设置,但会生成不同的版本子文件夹。
我希望这有帮助。 🙂
这就是你需要的: http : //social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/f89a00eb-9400-48ce-af20-cef78002c14e