编写涉及ServiceController的函数

时间:2011-05-19 15:21:45

标签: vb.net visual-studio-2010

我想从该块输出的文件中读取每行文本。 然后,每一行与服务控制器一起使用,以输出在该计算机上运行的服务列表。

Dim de As New DirectoryEntry()
    'Name place to write file to
    Dim strFile As String = "C:\DomainUsers.txt"
    Dim fileExists As Boolean = File.Exists(strFile)

    'get list of all users on domain and write to file
    de.Path = "WinNT://domain.com"
    'Write each user to a text file as List
    Using sw As New StreamWriter(File.Open(strFile, FileMode.OpenOrCreate))
        MsgBox("Writing all Users to Memory, Click OK and wait for Window to Close")
        For Each d As DirectoryEntry In de.Children()
            sw.WriteLine(d.Name)
        Next
        Console.Clear()
    End Using

    'Reopen File and read each line
    'Return List of Processes for each User in Overwritefashion to New Version of File
    Using sr As New StreamReader(File.Open(strFile, FileMode.Open))
        Dim InstalledServices As List(Of String) = GetInstalledServices(sr.ReadLine.Trim)
    End Using

End Sub

'Lost as to how to go about using this function...
Function GetInstalledServices(ByVal Computer As String) As List(Of String)

End Function

1 个答案:

答案 0 :(得分:0)

你必须检查它,但它会像......

Function GetInstalledServices(ByVal Computer As String) As List(Of String)

  Dim RetVal as New List(Of String)
  Dim scServices() As ServiceController()

  Try
      scServices = ServiceController.GetServices(Computer)

  For Each Svc as ServiceController In scServices

    RetVal.Add(Svc.ServiceName)

  Next

  Catch Ex as Exception
  End Try


  Return RetVal

End Function