安装期间初始化 user.config 或 app.exe.config
我正在开发一个 .NET WinForms 应用程序,它依赖于 user.config 来存储各种有用的设置,例如 Intranet Web 服务 URL。 我们希望能够在安装过程中导入自定义初始设置。 其用例是,如果一家公司想要在 100 台计算机上安装该软件,并且它们都应该引用相关的 Intranet Web 服务,则不需要在所有计算机上手动设置。
我们目前使用VS2008安装项目来生成安装包。
到目前为止,我们的想法是让安装程序能够使用信息(来自模板)创建自定义初始 .config 文件,然后将其放置到位,而不是安装项目安装的默认文件。
但是,除了这几种之外,我看不到其他方法:
- 将可执行文件名称硬编码到自定义安装程序操作中(派生自 System.Configuration.Install.Installer),以便使用 ConfigurationManager 类来确定 .config文件路径。 如果可执行文件名称发生更改,则需要手动更改安装程序。
- 滚动我们自己的配置类或文件/文件夹层次结构来存储设置。 如果我理解正确的话,这基本上会阻止我们利用 .NET 提供的隔离机制的优势(这也是这里的障碍)。
有谁知道任何替代方案,或者使上述两种方法变得可口的方法?
我担心答案可能是“不要这样做,它违背了机制的精神,推出你自己的设置导入机制”。
I am developing a .NET WinForms application which relies on user.config to store various useful settings such as intranet web service URLs. We would like to make it possible to import custom initial settings as part of the installation. The use case for this is if a company has 100 machines they want to install the software on, and they should all have a reference to the intranet web service in question, this should not need to be set up manually on all the machines.
We are currently using a VS2008 setup project to generate the install package.
Our idea so far has been to enable the installer to make a custom initial .config file with the information (from a template), and then put that in place instead of the default one that the setup project installs.
However, I can't see a way to do this other than these few:
- Hardcode the executable name into a custom installer action (deriving from System.Configuration.Install.Installer), in order to use the ConfigurationManager class to determine the .config file path. This would require manual changes to the installer if the executable name changes.
- Roll our own configuration classes or file/folder hierarchy to store the settings. This would basically bar us from the advantages of the isolation mechanism (which is also the obstacle here) that .NET provides, if I understand correctly.
Does anyone know of any alternatives, or of ways to make the above two palatable?
I fear the answer may be "don't do this, it defeats the spirit of the mechanism, roll your own settings import mechanism".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这并不完全是您正在寻找的答案,但您应该选择更“严肃”的安装程序。 我们正在使用高级安装程序,它非常棒,可以完成您能想到的任何事情。
I know that's not exactly the answer you're looking for, but you should opt for a more "serious" installer. We're using Advanced Installer and it's a blast, does anything and everything you can think of.
如果您可以在配置文件中对至少一项设置进行硬编码,则可以使用它来指向可以存储所有自定义配置设置的数据库或 Web 服务或网络文件路径。 使用 System.Configuration.ConfigurationManager.OpenExeConfiguration(),您可以在首次运行或每次应用程序启动时从此位置更新配置文件。
If it is acceptable for you to hardcode at least one setting in the config file, you can use this to point to a database or web service or network file path where all the custom config settings can be stored. Using System.Configuration.ConfigurationManager.OpenExeConfiguration() you can then update the config file from this location either on the first run or every time that the application starts up.
如果您为较大的公司创建此应用程序,那么您应该查看 组策略。 不幸的是,它会让您的应用程序回到注册表的黄金时代。 我知道,这将是您的程序的巨大变化,但这就是实现这一目标的方法。
但是,如果 Web 服务的 URL 是唯一的,您必须以这种方式进行设置,那么 GPO 可能就有点大材小用了。 在这种情况下,我会对可执行文件的名称进行硬编码。
If you create this application for larger companies, then you should have a look at Group Policy. Unfortunately, it'll bring your application back to the golden age of registry. I understand, that this would be a huge change in your program, but this is the way to this.
However, if the web service's URL is the only thing, you have to setup this way, then GPO might be an overkill. In that case I would hardcode the executable's name.