代码之家  ›  专栏  ›  技术社区  ›  raven

以编程方式从.NET卸载Windows服务

  •  0
  • raven  · 技术社区  · 16 年前

    在网上搜索信息之后,我成功地创建了一个服务,它可以根据命令行自行安装或卸载,也可以作为应用程序运行。

    但是,卸载代码无法正常工作。

    相关代码:

    Private Function UnInstallService(ByVal args As String(), ByRef errMsg As String) As Boolean
        Dim si As New ServiceInfo
    
        If (Not GetServiceInfo(args, si)) Then
            errMsg = "Error..."
            Return False
        End If
    
        If (Not IsServiceInstalled(si.Name)) Then
            errMsg = "Error..."
            Return False
        End If
    
        Try
            Dim installer As ServiceProcessInstaller = GetServiceInstaller(si)
            Dim stateSaver As IDictionary = New Hashtable
            Try
                installer.Uninstall(stateSaver)
            Catch e As exception
                errMsg = "Error..."
                Return False
            End Try
        Catch e As exception
            errMsg = "Error..."
            Return False
        End Try
    End Function
    
    Private Function GetServiceInstaller(ByVal si As ServiceInfo) As ServiceProcessInstaller
        Dim installer As ServiceInstaller = New ServiceInstaller()
        Dim pInstaller As New ServiceProcessInstaller
        pInstaller.Context = New InstallContext("", si.CommandLine)
    
        installer.Description = si.Description
        installer.DisplayName = si.DisplayName
        installer.ServiceName = si.Name
        installer.StartType = ServiceStartMode.Automatic
    
        If (si.Account = "LocalSystem") Then
            pInstaller.Account = ServiceAccount.LocalSystem
        ElseIf (si.Account = "LocalService") Then
            pInstaller.Account = ServiceAccount.LocalService
        ElseIf (si.Account = "NetworkService") Then
            pInstaller.Account = ServiceAccount.NetworkService
        Else
            pInstaller.Account = ServiceAccount.User
            pInstaller.Password = si.Password
            pInstaller.Username = si.Account
        End If
    
        pInstaller.Context.Parameters("assemblypath") = si.FullPath
    
        pInstaller.Installers.Add(installer)
        installer.Parent = pInstaller
    
        Return pInstaller
    End Function
    

    它在调用安装程序。卸载 安装的代码完全相同,只是检查服务是否已安装,并调用安装程序。安装然后安装程序.提交而不是卸载。我传递的参数完全相同。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Hans Olsson    16 年前

    你的代码似乎有点冗长,我要卸载的只是调用:

     Dim path As String = Assembly.GetExecutingAssembly().Location
     ManagedInstallerClass.InstallHelper(New String() {"/u", path})
    

    要安装,我只需执行以下操作:

     Dim path As String = Assembly.GetExecutingAssembly().Location
     ManagedInstallerClass.InstallHelper(New String() {path})
    

    然后在我的构造函数中有一些代码 ProjectInstaller 设置用户名等

    ManagedInstallerClass 引用如下: 此API支持.NET Framework基础结构,不打算直接从代码中使用。
    所以从你自己的代码中使用它可能不是未来的证明。。。

        2
  •  1
  •   raven    16 年前

    我在这里发现了问题: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/58505d7b-cb78-4486-88fc-9b86890664e0

    installer.Uninstall(stateSaver)
    

    相反,应该是

    installer.Uninstall(Nothing)
    
    推荐文章