如何在安装时设置应用程序设置(通过安装程序类)
我有一个包含 Installer 类的 Visual Studio 安装项目。 在安装程序类中,我设置了如下设置:
MessageBox.Show(Properties.Settings.Default.MySetting);
Properties.Settings.Default.MySetting = "Foo";
Properties.Settings.Default.Save();
MessageBox.Show(Properties.Settings.Default.MySetting);
问题是,即使我知道正在执行此代码(我正在做其他事情),但该设置从未设置!
消息框确实表明正在设置该值,但是当我转到 .config
文件时,该值仍然是空白!
有人知道为什么和/或可能的解决方法吗?
I have a Visual Studio setup project that has an Installer class. In the installer class I set a setting as follows:
MessageBox.Show(Properties.Settings.Default.MySetting);
Properties.Settings.Default.MySetting = "Foo";
Properties.Settings.Default.Save();
MessageBox.Show(Properties.Settings.Default.MySetting);
The problem is that even though I know that this code is being executed (I am doing other stuff), the setting is never set!!
The message boxes do suggest that the value is being set, but when I go to the .config
file the value is still blank!
Anyone have any ideas why and/or a possible workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我为安装程序所做的就是使用 App.Config 中的“文件”属性。 appSettings 块采用“文件”属性,如下所示:
“文件”属性有点像 CSS,因为最具体的设置获胜。 如果您在 user.config 和 App.config 中定义了“foo”,则使用 user.config 中的值。
然后,我有一个配置生成器,它使用字典中的值将第二个 appSettings 块写入 user.config (或任何您想要的名称)。
在您的安装程序中,只需在 Install 方法中添加类似以下内容:
我们将其用于测试、培训和生产服务器,因此我们所要做的就是在安装过程中指定计算机名称和密码,一切都已处理完毕为了我们。 过去这个过程需要 3 个小时,包括检查多个配置文件来设置密码。 现在它几乎完全自动化了。
希望这可以帮助。
What I do for my installers is to use the "file" attribute in App.Config. The appSettings block takes a "file" attribute, like so:
The "file" attribute is sort of like CSS, in that the most specific setting wins. If you have "foo" defined in user.config as well as App.config, the value in user.config is used.
Then, I have a config generator that writes out a second appSettings block to user.config (or whatever you want to call it), using values in a dictionary.
In your installer, just add something like the following in your Install method:
We use it for our test, training, and production servers, so all we have to do is specify the machine name and password during the install, and everything's taken care of for us. It used to be a 3-hour process, including going through multiple config files to set passwords. Now it's almost entirely automated.
Hope this helps.
最后我放弃了,并在安装应用程序后使用 RunOnce 类型的方法来执行此操作。
Well in the end I gave up and had a RunOnce type of method to do this stuff after the app was installed.
老实说,我不知道安装程序期间是否支持此功能 - 但如果支持,请确保您在
Settings.Default
上调用Save()
。I honestly don't know if this is supported during an installer - but if it is, make sure you're calling
Save()
onSettings.Default
.简而言之,安装程序类不支持它。 您只需要了解安装程序类方法是从系统目录运行的 msiexec.exe 调用的,并且该环境不可能知道您在目录中的某个位置有一个它完全不知道的设置文件。 这就是为什么它使用显式转到文件的安装位置并在那里更新它的代码。
The short answer is that it's not supported in installer classes. You just need to understand that installer class methods are called from msiexec.exe running from the system directory, and that environment can't possibly know that you have a settings file somewhere in a directory that it is completely unaware of. That's why it works with code that explicitly goes to the installed location of the file and updates it there.