ServiceController.GetServices 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
擷取電腦上的非裝置驅動程式服務,以及不是驅動程式的服務。
多載
GetServices(String) |
擷取指定電腦上除了裝置驅動程式服務以外的所有服務。 |
GetServices() |
擷取本機電腦上除了裝置驅動程式服務以外的所有服務。 |
GetServices(String)
擷取指定電腦上除了裝置驅動程式服務以外的所有服務。
public:
static cli::array <System::ServiceProcess::ServiceController ^> ^ GetServices(System::String ^ machineName);
public static System.ServiceProcess.ServiceController[] GetServices (string machineName);
static member GetServices : string -> System.ServiceProcess.ServiceController[]
Public Shared Function GetServices (machineName As String) As ServiceController()
參數
- machineName
- String
電腦,要從它擷取服務。
傳回
ServiceController 型別的陣列,其中每一元素都關聯到指定電腦上的服務。
例外狀況
存取系統 API 時發生的錯誤。
machineName
參數的語法無效。
備註
GetServices 只會傳回非設備磁碟機服務和不是指定電腦驅動程式的服務。 若要擷取設備磁碟機服務,請呼叫 GetDevices 方法。 這兩種方法一起提供電腦上所有服務的存取權。
另請參閱
適用於
GetServices()
擷取本機電腦上除了裝置驅動程式服務以外的所有服務。
public:
static cli::array <System::ServiceProcess::ServiceController ^> ^ GetServices();
public static System.ServiceProcess.ServiceController[] GetServices ();
static member GetServices : unit -> System.ServiceProcess.ServiceController[]
Public Shared Function GetServices () As ServiceController()
傳回
ServiceController 型別的陣列,其中每一元素都關聯到本機電腦上的服務。
例外狀況
存取系統 API 時發生的錯誤。
範例
下列範例會 ServiceController 使用 類別來顯示在本機電腦上執行的服務。
array<ServiceController^>^scServices = ServiceController::GetServices();
// Display the list of services currently running on this computer.
Console::WriteLine( "Services running on the local computer:" );
for each (ServiceController^ scTemp in scServices)
{
if ( scTemp->Status == ServiceControllerStatus::Running )
{
// Write the service name and the display name
// for each running service.
Console::WriteLine();
Console::WriteLine( " Service : {0}", scTemp->ServiceName );
Console::WriteLine( " Display name: {0}", scTemp->DisplayName );
// Query WMI for additional information about this service.
// Display the start name (LocalSystem, etc) and the service
// description.
ManagementObject^ wmiService;
String^ objPath;
objPath = String::Format( "Win32_Service.Name='{0}'", scTemp->ServiceName );
wmiService = gcnew ManagementObject( objPath );
if ( wmiService )
{
wmiService->Get();
Object^ objStartName = wmiService["StartName"];
Object^ objDescription = wmiService["Description"];
if ( objStartName )
{
Console::WriteLine( " Start name: {0}", objStartName->ToString() );
}
if ( objDescription )
{
Console::WriteLine( " Description: {0}", objDescription->ToString() );
}
}
}
}
ServiceController[] scServices;
scServices = ServiceController.GetServices();
// Display the list of services currently running on this computer.
Console.WriteLine("Services running on the local computer:");
foreach (ServiceController scTemp in scServices)
{
if (scTemp.Status == ServiceControllerStatus.Running)
{
// Write the service name and the display name
// for each running service.
Console.WriteLine();
Console.WriteLine(" Service : {0}", scTemp.ServiceName);
Console.WriteLine(" Display name: {0}", scTemp.DisplayName);
// Query WMI for additional information about this service.
// Display the start name (LocalSystem, etc) and the service
// description.
ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='" + scTemp.ServiceName + "'");
wmiService.Get();
Console.WriteLine(" Start name: {0}", wmiService["StartName"]);
Console.WriteLine(" Description: {0}", wmiService["Description"]);
}
}
Dim scServices() As ServiceController
scServices = ServiceController.GetServices()
' Display the list of services currently running on this computer.
Console.WriteLine("Services running on the local computer:")
Dim scTemp As ServiceController
For Each scTemp In scServices
If scTemp.Status = ServiceControllerStatus.Running Then
' Write the service name and the display name
' for each running service.
Console.WriteLine()
Console.WriteLine(" Service : {0}", scTemp.ServiceName)
Console.WriteLine(" Display name: {0}", scTemp.DisplayName)
' Query WMI for additional information about this service.
' Display the start name (LocalSystem, etc) and the service
' description.
Dim wmiService As ManagementObject
wmiService = New ManagementObject("Win32_Service.Name='" + scTemp.ServiceName + "'")
wmiService.Get()
Console.WriteLine(" Start name: {0}", wmiService("StartName"))
Console.WriteLine(" Description: {0}", wmiService("Description"))
End If
Next scTemp
備註
GetServices 只會傳回非設備磁碟機服務,以及不是來自本機電腦的驅動程式的服務。 若要擷取設備磁碟機服務,請呼叫 GetDevices 方法。 這兩種方法一起提供電腦上所有服務的存取權。