问题
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".
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/75546/initializing-user-config-or-app-exe-config-during-install