在MSI安装vb.net期间配置App.config应用程序设置

时间:2012-02-01 14:54:33

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

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

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

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

我的安装项目包含我的可执行文件,并且我的文本框将包含用户的一个变量。 setup project

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

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是我的变量名。 custom actions

我对安装程序代码非常挣扎......我想?

1 个答案:

答案 0 :(得分:0)

要考虑的一个选项是使用第三方安装程序,例如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。

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