简体   繁体   English

如何使用免费版Visual Studio在Windows Installer安装程序中更改VB .net应用程序的设置?

[英]How to alter a VB .net application's settings as part of a Windows Installer setup using free edition Visual Studio?

I want to alter some part of my application's <app>.exe.config file as part of the setup operation. 我想在安装操作中更改应用程序<app>.exe.config文件的某些部分。 For example, this could be the application's database connection string. 例如,这可以是应用程序的数据库连接字符串。

How do I do this, subject to the following constraints: 在遵守以下约束的情况下,我该如何做:

  1. With a minimum of extra components installed to Visual Studio. 至少要在Visual Studio中安装其他组件。 (Essentially just the installer templates) (基本上只是安装程序模板)
  2. With a minimum, preferrably no external tooling. 最少,最好没有外部工具。
  3. As a complete set of instructions. 作为一套完整的说明。
  4. Provide a simple working example. 提供一个简单的工作示例。

So far I have found several resources on the internet describing how to do this, but nobody gets it right. 到目前为止,我已经在Internet上找到了一些描述如何执行此操作的资源,但是没有人正确。 Everyone seems to forget or assume some steps are done, so blindly following any of the purported 'tutorials' does not get you a working installer. 每个人似乎都忘记或假设已经完成了某些步骤,因此盲目遵循任何所谓的“教程”都无法使您正常工作。

After much experimentation and piece-by-piece cobbling various online resources together, I have found a complete working solution. 经过大量的实验,并逐步将各种在线资源整合在一起,我找到了一个完整的工作解决方案。 As there are so many convoluted steps to get the final result: For the benefit of those interested, I'm sharing it here. 由于要完成最终的结果有很多复杂的步骤:为了使感兴趣的人受益,我在这里分享。

Throughout this explanation, <app> or [app] will be used to denote the application's name. 在整个说明中, <app>[app]将用于表示应用程序的名称。 I will use the latter brackets in those cases where using the former may create confusion. 在使用前者可能会造成混淆的情况下,我将使用后者。

Download Prerequisites 下载先决条件

First, make sure you only have one running instance of Visual Studio. 首先,确保只有一个正在运行的Visual Studio实例。

Next, you're going to want to open Tools > Extensions and Updates and download then install Microsoft Visual Studio 20xx Installer Projects . 接下来,您将要打开“ 工具”>“扩展和更新” ,然后下载并安装Microsoft Visual Studio 20xx安装程序项目

After the download completes, install it and restart Visual Studio. 下载完成后,安装它并重新启动Visual Studio。

Creating a basic Installer 创建一个基本的安装程序

Next, create a new Installer Project by adding a project of type Visual Studio Installer > Setup Project . 接下来,通过添加Visual Studio Installer> Setup Project类型的项目来创建新的Installer 项目 Add the project(s) whose outputs you want in the installer to the installer project. 将您想要在安装程序中输出的项目添加到安装程序项目中。 This creates a pre-configured simple msi installer file on build. 这将在构建时创建一个预配置的简单msi安装程序文件。

Next, right click your Solution in the Solution Explorer and go to Project Dependencies . 接下来,在解决方案资源管理器中右键单击您的解决方案,然后转到项目依赖项 Make sure you get your installer project to depend on your installed project. 确保使安装程序项目依赖于已安装的项目。

Then right click your Installer Project in the Solution Explorer and open the Configuration Manager from there (or access it from your solution or the title bar menu), then enable the installer project to Build . 然后在解决方案资源管理器中右键单击您的安装程序项目,然后从那里打开配置管理器(或从您的解决方案或标题栏菜单访问它),然后将安装程序项目启用为Build

Right click on the Installer Project and go to View file system . 右键单击Installer项目,然后转到“ 查看文件系统” Open the Application Folder on the left-hand panel then right click the folder to Add > Project Output... Select the Primary Output of your application. 打开左侧面板上的应用程序文件夹 ,然后右键单击该文件夹以添加>项目输出...选择应用程序的主输出 You can also add any auxillary files that are produced as part of the build process here. 您还可以在此处添加在构建过程中生成的所有辅助文件。

You now have a working, 'Testable' 1 simple setup after Building the solution. 构建解决方案后,您现在可以使用“可测试的” 1简单设置。

Adding a custom variable 添加自定义变量

In this example we will use a custom action to pass a simple text input to the new application's <app>.exe.config file. 在此示例中,我们将使用自定义操作将简单的文本输入传递给新应用程序的<app>.exe.config文件。

Our custom variable will be the path to the application's SQL server. 我们的自定义变量将是应用程序SQL服务器的路径。 I assume here that this variable is named "Server" in the <app>.exe.config file. 我在这里假定在<app>.exe.config文件中此变量名为“服务器”。

Right click the custom installer and go to View > Custom Actions . 右键单击自定义安装程序,然后转到查看>自定义操作 Add a custom Install action by right clicking that Folder then selecting Add . 通过右键单击该文件夹,然后选择添加添加自定义安装操作。 This opens a rather strange window that asks you to 'Select item in Project'. 这会打开一个非常奇怪的窗口,要求您“在Project中选择项目”。 Change the 'Look In' folder to point towards File System on Target Machine > Application Folder > [app] . 将“查找范围”文件夹更改为指向目标计算机上的文件系统>应用程序文件夹> [app] Click Add Output... and add the output of the main application whose settings you want to modify. 单击添加输出...,然后添加要修改其设置的主应用程序的输出。 2 The left hand window should now contain an item denoting Primary output from <app> (Active) 2现在,左侧窗口中应包含一个表示<app>的主要输出的项目(活动)

Select this and change the CustomActionData in the Properties window. 选择此项,然后在“ 属性”窗口中更改CustomActionData We change this value to be exactly /SqlPath=[SQLPATH] /TargetDir="[TARGETDIR]\\" . 我们将该值更改为/SqlPath=[SQLPATH] /TargetDir="[TARGETDIR]\\" The encoding for this data works as follows: 3 该数据的编码如下: 3

Start with a single forward slash "/" 
Encode the variables as [name-in-installer-class]=[name-in-installer-project]
If your name in the installer project contains spaces you must surround it with double quotes
Separate each variable with a space and forward-slash or " /"
TARGETDIR is a special variable and is encoded as [name-in-installer-class]="[TARGETDIR]\"

Navigate to View > User Interface in the Installer project. 导航到Installer项目中的View> User Interface Under Install > Start add a new form of type Textboxes (A) . 安装>开始下,添加Textboxes (A)类型的新形式。 In the properties window, disable the bottom 3 textboxes, then set these variables: 在属性窗口中,禁用底部的3个文本框,然后设置以下变量:

  1. BannerText , to the window title (eg "Database") BannerText ,到窗口标题(例如“数据库”)
  2. BodyText , an optional extra description of this setup step BodyText ,此设置步骤的可选附加说明
  3. Edit1Label , "SQL server path" Edit1Label ,“ SQL服务器路径”
  4. Edit1Property , exactly "SQLPATH" Edit1Property ,完全是“ SQLPATH”
  5. Edit1Value The default value for the SQL server path. Edit1Value SQL Server路径的默认值。

Next, add an installer class to the project we want to modify settings of. 接下来,将安装程序类添加到我们要修改其设置的项目中。 Add > New Item then search the templates for 'Installer Class'. 添加>新建项目,然后在模板中搜索“安装程序类”。 Customize it as follows. 如下自定义。 (Replace 'APPNAME' in the code with the name of the executable): (将代码中的“ APPNAME”替换为可执行文件的名称):

Imports System.ComponentModel
Imports System.Configuration

Public Class clsInstaller
    Inherits System.Configuration.Install.Installer

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add initialization code after the call to InitializeComponent

    End Sub

    Public Overrides Sub Install(ByVal stateServer As System.Collections.IDictionary)
        MyBase.Install(stateServer)
        ' Change the Application settings' 'database connection' setting. 
        ' Note: targetDirectory contains an extra '\' at the end. 
        Dim targetDirectory As String = Context.Parameters("TargetDir")
        Dim str As String = ""
        'For some reason custom fields are sent escaped to the installer class. 
        'This undoes the escaping of the directory slashes.
        Dim sqlServerPath As String = Context.Parameters("SqlPath").Replace("\\", "\")
        Dim executablePath As String = String.Format("{0}APPNAME.exe",
            targetDirectory.Substring(0, targetDirectory.Length - 1))
        Dim config As Configuration = 
           ConfigurationManager.OpenExeConfiguration(executablePath)
        If config Is Nothing Then
            MessageBox.Show("Failed to load configuration file", "Error", 
                MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        Dim sectionGroup As ConfigurationSectionGroup = 
           config.GetSectionGroup("applicationSettings")
        Dim configSection As ConfigurationSection = 
           sectionGroup.Sections("APPNAME.My.MySettings")
        Dim settingsSection As ClientSettingsSection = 
           CType(configSection, ClientSettingsSection)
        Dim serverSetting As SettingElement = 
           settingsSection.Settings.Get("Server")
        serverSetting.Value.ValueXml.InnerText = sqlServerPath
        configSection.SectionInformation.ForceSave = True
        config.Save()
    End Sub
End Class

Build the solution, and the installer should now modify the application's settings file! 构建解决方案,安装程序现在应该修改应用程序的设置文件!

Notes 笔记

1 : In the sense that you can manually run it. 1 :可以手动运行。 Actually debugging requires instantiating it from the setup. 实际上调试需要从安装程序中实例化它。 See this question . 看到这个问题

2 : It's also possible to add a separate project here to create a separate dll whose purpose is to provide the custom code entry point. 2 :也可以在此处添加一个单独的项目来创建一个单独的dll,其目的是提供自定义代码入口点。 This has better performance characteristics but is avoided in this example because of increased complexity (you would have to get one program to modify another program's settings file). 这具有更好的性能特征,但是在本示例中避免了这种情况,因为它增加了复杂性(您必须获得一个程序才能修改另一个程序的设置文件)。

3 :For more information about this encoding consult the article about CustomActionData Property on MSDN. 3 :有关此编码的更多信息,请参见MSDN上有关CustomActionData属性的文章。

4 : The ConfigurationManager section group and section names are case sensitive, so make sure they exactly match as they are in the actual setting XML file. 4ConfigurationManager节组和节名称区分大小写,因此请确保它们与实际设置XML文件中的内容完全匹配。

暂无
暂无

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

相关问题 如何使用Visual Studio为.net Windows服务创建安装程序 - How to create an installer for a .net Windows Service using Visual Studio 使用.NET Windows Installer安装VB6应用程序和dll - Installing VB6 application and dlls using .net windows installer 如何使用 Visual Studio 安装程序项目安装 .NET 6 Windows 服务(Worker Service)? - How do you install a .NET 6 Windows Service (Worker Service) using a Visual Studio Installer project? Visual Studio 2008 Express Edition,Winforms中的应用程序设置 - Application settings in visual studio 2008 express edition, winforms Visual Studio Installer设置应用程序,框架和第三方工具安装程序 - Visual Studio Installer To Set Application, Framework And The Third Party Tool Setup 如何使用Visual Studio中的安装项目安装程序在IIS中发布ASP Web应用程序 - How to publish an asp web application in IIS with setup project installer in visual studio ClickOnce应用程序设置与管理员权限(Visual Studio,VB.NET) - ClickOnce application Settings vs Administrator Rights (Visual Studio, VB.NET) 安装需要.Net Famework Verison 3.5。 Visual Studio 安装程序项目,即使在 windows 功能中启用了 3.5 - Setup requires.Net Famework Verison 3.5. Visual Studio Installer project, Even though 3.5 is enabled in windows feature 如何使用.net确定已安装的Windows Edition - How to determine installed Windows Edition using .net 如何在安装时更改.NET应用程序/用户设置? - How do I alter a .NET application/user settings on installation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM