【问题标题】:Configure App.config Application Settings During MSI Install vb.net在 MSI 安装 vb.net 期间配置 App.config 应用程序设置
【发布时间】:2012-02-24 06:12:53
【问题描述】:

我正在尝试为我的 Windows 服务创建一个 msi 安装。创建 msi 的原因是目标用户希望能够在尽可能少的干预的情况下快速安装服务。

我可以将服务安装为 msi,但我的代码中有一个变量,我需要用户定义何时安装 msi。我需要用户的变量是他们希望我的服务创建的 xml 文件所在的文件路径。

我想我可以配置 app.config 应用程序设置以包含 xml 文件应写入的文件路径。但是我正在努力做到这一点,我不确定这是否是最好的方法?

我有我的安装项目,其中包含我的可执行文件,并且我的文本框将包含来自用户的一个变量。

我有一个安装程序类,其中包含我的服务安装程序和进程安装程序。这就是我努力理解下一步我需要做什么的地方。 我需要覆盖安装方法吗?我的安装程序类的当前代码是自动生成的,如下:

Imports System.Configuration.Install
Imports System.Configuration

<System.ComponentModel.RunInstaller(True)> Partial Class ProjectInstaller
Inherits System.Configuration.Install.Installer

'Installer overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try
End Sub

'Required by the Component Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.  
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.ServiceProcessInstaller1 = New System.ServiceProcess.ServiceProcessInstaller()
    Me.ServiceInstaller1 = New System.ServiceProcess.ServiceInstaller()
    '
    'ServiceProcessInstaller1
    '
    Me.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem
    Me.ServiceProcessInstaller1.Password = Nothing
    Me.ServiceProcessInstaller1.Username = Nothing
    '
    'ServiceInstaller1
    '
    Me.ServiceInstaller1.ServiceName = "Spotter"
    Me.ServiceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic
    '
    'ProjectInstaller
    '
    Me.Installers.AddRange(New System.Configuration.Install.Installer() {Me.ServiceProcessInstaller1, Me.ServiceInstaller1})

End Sub

Friend WithEvents ServiceProcessInstaller1 As System.ServiceProcess.ServiceProcessInstaller
Friend WithEvents ServiceInstaller1 As System.ServiceProcess.ServiceInstaller

End Class

我什至可以添加 CustomActionData 值。该字符串确定传递给我用来收集输入的用户值的上下文对象的内容。 param1 是我的变量名。

我在安装程序代码方面非常苦恼……我想?

【问题讨论】:

    标签: vb.net windows-services windows-installer app-config


    【解决方案1】:

    要考虑的一个选项是使用第三方安装程序,例如 Installshield,它内置支持修改 xml 配置文件,例如配置文件。

    但是,如果你想自己滚动,你肯定需要重写 Install 方法。您在 CustomData 中传递的任何参数都将在作为参数传递给此方法的字典中可用。

    例如:

    Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
        MyBase.Install(stateSaver)
    
        If Me.Context.Parameters.Count <> 0 Then
    
            For Each sKey As String In Context.Parameters.Keys
                Select Case sKey.ToUpper
                    Case "PARAM1"
                        ' XML directory
                        Me.XMLDir = Context.Parameters(sKey)
    
                End Select
            Next
        End If
    End Sub
    

    在这种情况下,我们总是将值写入注册表,以便用户在卸载或重新安装时不必重新输入。

    我不确定修改 app.config 的确切事件顺序,但您可以写入注册表,然后在服务首次启动时修改 app.config。

    您可能还会发现,您需要从提交阶段删除自定义操作,以便安装程序成功运行。

    【讨论】:

    • 感谢您的回复。通读您的示例,我正在努力完全理解它。 Me.XMLDir 变量是什么?
    • 该变量只是为了显示提取自定义数据中传递的值。这是您将用来更新 app.config 的内容。
    猜你喜欢
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 2016-09-21
    • 1970-01-01
    相关资源
    最近更新 更多