如何在安装过程中修改 .NET 配置文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/289773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:36:49  来源:igfitidea点击:

How to modify .NET config files during installation?

.netconfigurationinstaller

提问by Tomas Pajonk

I use the app.config file to store some values (path to a mapping database, data connection selections). These settings differ on the user machines and I would like the installer to set them right. Is there an installer that can work with .NET config files during setup and allow me to create some dialogs that would help me fill in these values ?

我使用 app.config 文件来存储一些值(映射数据库的路径、数据连接选择)。这些设置在用户机器上有所不同,我希望安装程序正确设置它们。是否有安装程序可以在安装过程中使用 .NET 配置文件并允许我创建一些对话框来帮助我填写这些值?

I know this question may be similar to : Initializing user.config or app.exe.config during install, but I am not limited to VS 2008 setup project and I want to change the settings in the config files.

我知道这个问题可能类似于:在安装期间初始化 user.config 或 app.exe.config,但我不限于 VS 2008 安装项目,我想更改配置文件中的设置。

EDIT: I see that using WIX is one option, but I feel like cracking a walnut with a sledgehammer. It may be the only solution, but I still hope for something simple.

编辑:我看到使用 WIX 是一种选择,但我觉得像用大锤敲碎核桃。这可能是唯一的解决方案,但我仍然希望有一些简单的方法。

回答by CheGueVerra

We use WIX to change the application's configuration file. It works really well, you'll need to add wixUtilExtension.dll in the reference.

我们使用 WIX 来更改应用程序的配置文件。它工作得非常好,您需要在参考中添加 wixUtilExtension.dll。

WIX sample:

WIX 示例:

<Component Id="ChangeConfig" Guid="[YOUR_GUID_HERE]">
   <File Id="App.config" Name="MyApplication.exe.config" Vital="yes" KeyPath="yes" Source="[Path to project dir]\app.config" />
   <util:XmlFile Id="AppConfigSetConnStr" Action="setValue" Permanent="yes" File="[#App.config]"            
        ElementPath="/configuration/connectionStrings/add[\[]@name='MyDatabaseName'[\]]" Name="connectionString" 
        Value="Your Connection string values here" />

</Component>

It actually depends on what you are using to create the installer, What are you using ?
Have alook at the WIX Tutorial.

这实际上取决于您使用什么来创建安装程序,您在使用什么?
看看WIX 教程

回答by GalacticCowboy

If you're using a VS setup project, have you created a custom action? I've used them for everything from poking XML values to deploying databases.

如果您使用的是 VS 安装项目,您是否创建了自定义操作?我已经将它们用于从查看 XML 值到部署数据库的所有事情。

回答by tgeros

I've used the WIX toolset to produce an msi. The tool allows you to declaratively specify changes to XML files like app.config during installation. Problem is though there is a significant learning curve. Search sourceforge for wix.

我已经使用 WIX 工具集生成了一个 msi。该工具允许您在安装期间以声明方式指定对 XML 文件(如 app.config)的更改。问题是虽然有一个显着的学习曲线。在 sourceforge 中搜索 wix。

回答by ine

I used NSIS with an XML plugin to do this.

我使用带有 XML 插件的 NSIS 来做到这一点。

回答by sjclark76

I know this is an old question but we didn't want to use WIX either for a very simple little installer. I found this article that explains in a lot of detail exactly how to do it. It worked perfectly for us.

我知道这是一个老问题,但我们也不想将 WIX 用于一个非常简单的小安装程序。我发现这篇文章详细解释了如何做到这一点。它非常适合我们。

http://www.c-sharpcorner.com/UploadFile/ddoedens/CustomUI11102005042556AM/CustomUI.aspx

http://www.c-sharpcorner.com/UploadFile/ddoedens/CustomUI11102005042556AM/CustomUI.aspx