简体   繁体   English

使用Microsoft Visual Studio安装程序项目构建MSI时如何扩展user.config设置

[英]How to extend user.config settings when building an MSI using Microsoft Visual Studio Installer Projects

As the title suggests, I'm attempting to extend my user.config XML file that is being created in my main project to my adjacent setup project. 顾名思义,我正在尝试将在主项目中创建的user.config XML文件扩展到相邻的安装项目。 I'm building an MSI using Microsoft Visual Studio 2017 Installer Projects (link) and also using Visual Studio 2017 to create a simple form application. 我正在使用Microsoft Visual Studio 2017安装程序项目(链接)构建一个MSI,还使用Visual Studio 2017创建一个简单的表单应用程序。

As an example, here are my two projects- 举例来说,这是我的两个项目-

在此处输入图片说明

Within my main Windows Form project, I have a bool property called testBool that is set to false by default. 在我的主要Windows Form项目中,我有一个名为testBool的bool属性,默认情况下将其设置为false。

在此处输入图片说明

From my form I have a button that runs the following simple code and sets the value of testBool to true- 从我的表单中,我有一个运行以下简单代码的按钮,并将testBool的值设置为true-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

namespace TestUserConfig
{

public partial class Form1 : Form
    {
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("testBool = " + Properties.Settings.Default.testBool.ToString());

        //setting the property to true
        Properties.Settings.Default.testBool = true;

        MessageBox.Show("Test Bool = " + Properties.Settings.Default.testBool.ToString());

        //save the 
        Properties.Settings.Default.Save();

        //location of the user.config file (reference to System.Configuration required)
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
        MessageBox.Show(config.FilePath);
    }
    }
}

This all works as expected and outputs the user.config file to the following path- C:\\Users\\USERNAME\\AppData\\Local\\Microsoft_Corporation\\PROJECTNAME_umi0kjiiephephcroktxjbzai0nh\\14.0.7208.5000\\user.config 所有这些均按预期工作,并将user.config文件输出到以下路径-C:\\ Users \\ USERNAME \\ AppData \\ Local \\ Microsoft_Corporation \\ PROJECTNAME_umi0kjiiephephcroktxjbzai0nh \\ 14.0.7208.5000 \\ user.config

The issue I'm having is when I build my MSI the same code does not run as I would expect. 我遇到的问题是,在构建MSI时,相同的代码未按预期运行。 In fact the MessageBox.Show's do not even run with the exception of the last line of code – “MessageBox.Show("Hello!!”);” 实际上,除了最后一行代码“ MessageBox.Show(“ Hello !!”);”外,MessageBox.Show甚至都没有运行。

I would expect the remaining message boxes to at least output a blank messagebox window. 我希望其余的消息框至少输出一个空白的消息框窗口。

Also, in the MSI build directory (c:\\ Program Files (x86)\\install dir) I see a file with the name “PROJECTNAME.dll.config” that contain the default value for testBool. 另外,在MSI构建目录(c:\\ Program Files(x86)\\ install dir)中,我看到了一个名为“ PROJECTNAME.dll.config”的文件,其中包含testBool的默认值。
If I publish a click-once application the application works fine. 如果我发布一个单击应用程序,则该应用程序运行良好。 Is there something I need to add to my setup project to make this work? 我需要添加一些东西到我的安装项目中来完成这项工作吗?

I've read every post I can find on the matter and can't seem to locate a suitable answer. 我已阅读了有关此事的所有帖子,但似乎找不到合适的答案。 I don't really want to invest tons of time into learning WIX although I might need to if that's the only route to take. 我真的不想花费大量时间来学习WIX,尽管如果这是唯一的方法,我可能会需要。

There a couple of issues with this approach: 这种方法存在两个问题:

  1. Managed code custom actions are not instantiated the same as when run as an application. 托管代码自定义操作的实例化与作为应用程序运行时的实例化不同。 There is no native Windows Installer support for managed code, so Visual Studio setups include a C++ Dll as a shim between the running installer and your managed code. Windows对托管代码不提供本机Windows Installer支持,因此Visual Studio安装程序包括C ++ Dll作为正在运行的安装程序和托管代码之间的隔离程序。 This shim loads and instantiates your managed code with reflection, bypassing the config settings stuff. 该填充程序将通过反射加载并实例化托管代码,从而绕过配置设置。 This means you need to use Xml parsing to access the file. 这意味着您需要使用Xml解析来访问文件。

  2. Despite the fact that Visual Studio lets you do it, you should not be showing UI in the setup out of a Visual Studio custom action. 尽管Visual Studio允许您执行此操作,但您不应在Visual Studio自定义操作之外的安装程序中显示UI。 It breaks silent install, group policy deployment, and the Windows Installer architecture isn't prepared to deal with UI in that stage of the install. 它破坏了静默安装,组策略部署,并且Windows Installer体系结构不准备在安装的那个阶段处理UI。 The specific message problem is that the apartment threading doesn't have a proper UI thread. 特定的消息问题是,单元线程没有正确的UI线程。 So your messageboxes won't work properly. 因此您的消息框将无法正常工作。 It's not obvious from your code what you are trying to do (because it looks like a test that just sets the value and stores it) but if you had a Windows form with buttons and so on, they wouldn't work either. 从代码中并不清楚要做什么(因为它看起来像是一个测试,只设置了值并存储了它),但是如果您有一个带有按钮等的Windows窗体,它们也不起作用。

If you want to get a value from a UI during the install, then use one of the additional UI forms (such as TextBoxes) and get the value from there. 如果要在安装过程中从UI获取值,请使用其他UI窗体之一(例如TextBoxes)并从那里获取值。 Then the property (default name such as EDITA1) can be passed in CustomActionData into a custom action that sets the value in the file using Xml classes. 然后,可以在CustomActionData中将属性(默认名称,例如EDITA1)传递到一个自定义操作中,该操作使用Xml类在文件中设置值。 On the other hand, if this is the kind of setting used by the running app, then it's typically more appropriate to have the app do it at first run. 另一方面,如果这是正在运行的应用程序使用的设置类型,则通常更适合让该应用程序在首次运行时执行该设置。

Another potential issue is access: files in Program Files cannot be created or updated by limited users, so trying to update settings files there can be an issue. 另一个潜在的问题是访问:程序文件中的文件不能由受限用户创建或更新,因此尝试更新设置文件可能会出现问题。 So I would not store settings in Program Files unless they are read only. 因此,除非设置是只读的,否则我不会将设置存储在程序文件中。 If that's an issue, just have any Xml config files in (say) User's Application Data folder (that's the location in the Visual Studio setup project's file system view). 如果这是一个问题,则只需在(例如)“用户的应用程序数据”文件夹(即Visual Studio设置项目的文件系统视图中的位置)中放置任何Xml配置文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft Visual Studio 安装程序项目 - 如何向使用 Process.Start() 执行但没有修复选项的 MSI 提供重新启动提示 - Microsoft Visual Studio Installer Projects - how to provide restart prompt to MSI executed with Process.Start(), but without Repair option 在Visual Studio调试期间从哪里加载user.config - Where is user.config loaded from during Visual Studio debugging 在Visual Studio 2017中使用构建事件时的msi安装程序 - msi installer when using build events in Visual Studio 2017 当版本号更改时,如何升级user.config中的所有设置? - How do you upgrade all settings in user.config when the version number changes? 如何查看user.config文件并在更改后重新加载设置? - How can I watch the user.config file and reload the settings when it changes? 如何在 Visual Studio 中创建 MSI 安装程序? - How to Create an MSI Installer in Visual Studio? 如何将“Microsoft Visual Studio 安装程序项目”添加到 Visual Studio 2017? - How Can Add "Microsoft Visual Studio Installer Projects" to Visual Studio 2017? 安装后使用“Microsoft Visual Studio 安装程序项目”安装 FTDI 驱动程序(自定义操作) - Using "Microsoft Visual Studio Installer Projects" to Install FTDI Driver after Installation (Custom Action) 使用“运行...”创建一个不同的user.config - Using “Run…” creates a different user.config User.config是如何创建的以及如何使用它 - How is User.config created and how it is used
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM