如何使用免费版 Visual Studio 更改 VB .net 应用程序的设置作为 Windows 安装程序设置的一部分?
How to alter a VB .net application's settings as part of a Windows Installer setup using free edition Visual Studio?
作为设置操作的一部分,我想更改应用程序 <app>.exe.config
文件的某些部分。例如,这可能是应用程序的数据库连接字符串。
如何执行此操作,但要遵守以下限制条件:
- 在 Visual Studio 上安装了最少的额外组件。 (基本上只是安装程序模板)
- 最少,最好没有外部工具。
- 作为一套完整的说明。
- 提供一个简单的工作示例。
到目前为止,我已经在 Internet 上找到了一些描述如何执行此操作的资源,但没有人能正确使用。每个人似乎都忘记或假设已经完成了一些步骤,因此盲目地遵循任何所谓的 'tutorials' 并不能使您获得有效的安装程序。
经过多次试验并 piece-by-piece 将各种在线资源拼凑在一起,我找到了一个完整的工作解决方案。由于获得最终结果的步骤非常复杂:为了感兴趣的人的利益,我在这里分享。
在整个解释过程中,<app>
或 [app]
将用于表示应用程序的名称。在使用前者可能会造成混淆的情况下,我将使用后者括号。
下载先决条件
首先,确保您只有一个 运行ning Visual Studio 实例。
接下来,您需要打开 工具 > 扩展和更新 并下载然后安装 Microsoft Visual Studio 20xx 安装程序项目。
下载完成后,安装并重启Visual Studio。
创建基本安装程序
接下来,通过添加 Visual Studio 安装程序 > 安装程序项目 类型的项目来创建新的安装程序项目。将安装程序中需要其输出的项目添加到安装程序项目。这会在构建时创建一个 pre-configured 简单的 msi 安装程序文件。
接下来,在解决方案资源管理器中右键单击您的解决方案,然后转到 项目依赖项。确保您的安装程序项目依赖于您安装的项目。
然后在解决方案资源管理器中右键单击您的安装程序项目并从那里打开配置管理器(或从您的解决方案或标题栏菜单访问它),然后启用安装程序项目 Build。
右键单击安装程序项目并转到查看文件系统。在 left-hand 面板上打开 Application Folder,然后右键单击该文件夹以 Add > Project Output... Select您的应用程序的主要输出。您还可以在此处添加作为构建过程的一部分生成的任何辅助文件。
在构建解决方案后,您现在有一个有效的 'Testable'1 简单设置。
添加自定义变量
在此示例中,我们将使用自定义操作将简单的文本输入传递到新应用程序的 <app>.exe.config
文件。
我们的自定义变量将是应用程序 SQL 服务器的路径。我在这里假设这个变量在 <app>.exe.config
文件中被命名为 "Server"。
右键单击自定义安装程序并转到查看 > 自定义操作。通过右键单击该文件夹然后选择 Add 添加自定义 Install 操作。这会打开一个相当奇怪的 window,要求您 'Select item in Project'。将 'Look In' 文件夹更改为指向 目标计算机上的文件系统 > 应用程序文件夹 > [app]。单击 添加输出... 并添加要修改其设置的主应用程序的输出。2 左手 window现在应该包含一个项目,表示 的主要输出(活动)
Select 并更改 Properties window 中的 CustomActionData
。我们将此值更改为正好 /SqlPath=[SQLPATH] /TargetDir="[TARGETDIR]\"
。此数据的编码如下: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]\"
导航到安装程序项目中的查看 > 用户界面。在 安装 > 开始 下添加类型 Textboxes (A)
的新表单。在属性 window 中,禁用底部的 3 个文本框,然后设置这些变量:
BannerText
,到 window 标题(例如 "Database")
BodyText
,此设置步骤的可选额外说明
Edit1Label
、"SQL server path"
Edit1Property
,恰好 "SQLPATH"
Edit1Value
SQL 服务器路径的默认值。
接下来,将安装程序class 添加到我们要修改其设置的项目中。 添加 > 新项目 然后在模板中搜索 'Installer Class'。自定义如下。 (将代码中的 '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
构建解决方案,安装程序现在应该修改应用程序的设置文件!
备注
1:从某种意义上说,你可以手动运行它。实际上调试需要从设置中实例化它。参见 this question。
2:也可以在这里添加一个单独的项目来创建一个单独的dll,其目的是提供自定义代码入口点。这具有更好的性能特征,但由于增加了复杂性(您必须让一个程序修改另一个程序的设置文件)。
3:有关此编码的更多信息,请参阅 MSDN 上有关 CustomActionData Property 的文章。
4: ConfigurationManager
节组和节名区分大小写,因此请确保它们与实际设置中的完全匹配 XML文件。
作为设置操作的一部分,我想更改应用程序 <app>.exe.config
文件的某些部分。例如,这可能是应用程序的数据库连接字符串。
如何执行此操作,但要遵守以下限制条件:
- 在 Visual Studio 上安装了最少的额外组件。 (基本上只是安装程序模板)
- 最少,最好没有外部工具。
- 作为一套完整的说明。
- 提供一个简单的工作示例。
到目前为止,我已经在 Internet 上找到了一些描述如何执行此操作的资源,但没有人能正确使用。每个人似乎都忘记或假设已经完成了一些步骤,因此盲目地遵循任何所谓的 'tutorials' 并不能使您获得有效的安装程序。
经过多次试验并 piece-by-piece 将各种在线资源拼凑在一起,我找到了一个完整的工作解决方案。由于获得最终结果的步骤非常复杂:为了感兴趣的人的利益,我在这里分享。
在整个解释过程中,<app>
或 [app]
将用于表示应用程序的名称。在使用前者可能会造成混淆的情况下,我将使用后者括号。
下载先决条件
首先,确保您只有一个 运行ning Visual Studio 实例。
接下来,您需要打开 工具 > 扩展和更新 并下载然后安装 Microsoft Visual Studio 20xx 安装程序项目。
下载完成后,安装并重启Visual Studio。
创建基本安装程序
接下来,通过添加 Visual Studio 安装程序 > 安装程序项目 类型的项目来创建新的安装程序项目。将安装程序中需要其输出的项目添加到安装程序项目。这会在构建时创建一个 pre-configured 简单的 msi 安装程序文件。
接下来,在解决方案资源管理器中右键单击您的解决方案,然后转到 项目依赖项。确保您的安装程序项目依赖于您安装的项目。
然后在解决方案资源管理器中右键单击您的安装程序项目并从那里打开配置管理器(或从您的解决方案或标题栏菜单访问它),然后启用安装程序项目 Build。
右键单击安装程序项目并转到查看文件系统。在 left-hand 面板上打开 Application Folder,然后右键单击该文件夹以 Add > Project Output... Select您的应用程序的主要输出。您还可以在此处添加作为构建过程的一部分生成的任何辅助文件。
在构建解决方案后,您现在有一个有效的 'Testable'1 简单设置。
添加自定义变量
在此示例中,我们将使用自定义操作将简单的文本输入传递到新应用程序的 <app>.exe.config
文件。
我们的自定义变量将是应用程序 SQL 服务器的路径。我在这里假设这个变量在 <app>.exe.config
文件中被命名为 "Server"。
右键单击自定义安装程序并转到查看 > 自定义操作。通过右键单击该文件夹然后选择 Add 添加自定义 Install 操作。这会打开一个相当奇怪的 window,要求您 'Select item in Project'。将 'Look In' 文件夹更改为指向 目标计算机上的文件系统 > 应用程序文件夹 > [app]。单击 添加输出... 并添加要修改其设置的主应用程序的输出。2 左手 window现在应该包含一个项目,表示
Select 并更改 Properties window 中的 CustomActionData
。我们将此值更改为正好 /SqlPath=[SQLPATH] /TargetDir="[TARGETDIR]\"
。此数据的编码如下: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]\"
导航到安装程序项目中的查看 > 用户界面。在 安装 > 开始 下添加类型 Textboxes (A)
的新表单。在属性 window 中,禁用底部的 3 个文本框,然后设置这些变量:
BannerText
,到 window 标题(例如 "Database")BodyText
,此设置步骤的可选额外说明Edit1Label
、"SQL server path"Edit1Property
,恰好 "SQLPATH"Edit1Value
SQL 服务器路径的默认值。
接下来,将安装程序class 添加到我们要修改其设置的项目中。 添加 > 新项目 然后在模板中搜索 'Installer Class'。自定义如下。 (将代码中的 '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
构建解决方案,安装程序现在应该修改应用程序的设置文件!
备注
1:从某种意义上说,你可以手动运行它。实际上调试需要从设置中实例化它。参见 this question。
2:也可以在这里添加一个单独的项目来创建一个单独的dll,其目的是提供自定义代码入口点。这具有更好的性能特征,但由于增加了复杂性(您必须让一个程序修改另一个程序的设置文件)。
3:有关此编码的更多信息,请参阅 MSDN 上有关 CustomActionData Property 的文章。
4: ConfigurationManager
节组和节名区分大小写,因此请确保它们与实际设置中的完全匹配 XML文件。