Process.MachineName 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得相關處理序正在執行的所在電腦的名稱。
public:
property System::String ^ MachineName { System::String ^ get(); };
public string MachineName { get; }
[System.ComponentModel.Browsable(false)]
public string MachineName { get; }
member this.MachineName : string
[<System.ComponentModel.Browsable(false)>]
member this.MachineName : string
Public ReadOnly Property MachineName As String
屬性值
相關處理序正在執行的所在電腦的名稱。
- 屬性
例外狀況
沒有任何與這個 Process 物件關聯的處理序。
範例
若要使用下列範例,您必須先在遠端電腦上啟動至少一個記事本實例。 此範例會要求執行記事本的遠端電腦名稱,然後顯示每個實例的個別 ProcessName、 Id和 MachineName 屬性。
using System;
using System.Diagnostics;
class GetProcessesByNameClass
{
public static void Main(string[] args)
{
Console.WriteLine("Create notepad processes on remote computer");
Console.Write("Enter remote computer name : ");
string remoteMachineName = Console.ReadLine();
if (remoteMachineName == null)
{
// Prepend a new line to prevent it from being on the same line as the prompt.
Console.WriteLine(Environment.NewLine + "You have to enter a remote computer name.");
return;
}
try
{
// Get all notepad processess into Process array.
Process[] myProcesses = Process.GetProcessesByName("notepad", remoteMachineName);
if (myProcesses.Length == 0)
Console.WriteLine("Could not find notepad processes on remote computer.");
foreach (Process myProcess in myProcesses)
{
Console.WriteLine(
$"Process Name : {myProcess.ProcessName}\n" +
$"Process ID : {myProcess.Id}\n" +
$"MachineName : {myProcess.MachineName}");
}
}
catch (ArgumentException)
{
Console.WriteLine($"The value \'{remoteMachineName}\' is an invalid remote computer name.");
}
catch (InvalidOperationException)
{
Console.WriteLine("Unable to get process information on the remote computer.");
}
catch (PlatformNotSupportedException)
{
Console.WriteLine(
"Finding notepad processes on remote computers " +
"is not supported on this operating system.");
}
}
}
Imports System.Diagnostics
Class GetProcessesByNameClass
'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
Main(Environment.GetCommandLineArgs())
End Sub
Public Overloads Shared Sub Main(ByVal args() As String)
Console.WriteLine("Create notepad processes on remote computer")
Console.Write("Enter remote computer name : ")
Dim remoteMachineName As String = Console.ReadLine()
If remoteMachineName Is Nothing Then
' Prepend a new line to prevent it from being on the same line as the prompt.
Console.WriteLine(Environment.NewLine + "You have to enter a remote computer name.")
Return
End If
Try
' Get all notepad processess into Process array.
Dim myProcesses As Process() = Process.GetProcessesByName _
("notepad", remoteMachineName)
If myProcesses.Length = 0 Then
Console.WriteLine("Could not find notepad processes on remote computer.")
End If
Dim myProcess As Process
For Each myProcess In myProcesses
Console.WriteLine("Process Name : " & myProcess.ProcessName &
" Process ID : " & myProcess.Id &
" MachineName : " & myProcess.MachineName)
Next myProcess
Catch e As ArgumentException
Console.WriteLine("The value '" & remoteMachineName & "' is an invalid remote computer name.")
Catch e As PlatformNotSupportedException
Console.WriteLine(
"Finding notepad processes on remote computers " &
"is not supported on this operating system.")
Catch e As InvalidOperationException
Console.WriteLine("Unable to get process information on the remote computer.")
End Try
End Sub
End Class
備註
您可以檢視在遠端電腦上執行之進程的統計數據和處理資訊,但無法呼叫 Start、 CloseMainWindow或 Kill 遠端電腦上。
注意
當相關聯的進程在本機計算機上執行時,這個屬性會傳回句點 (“。”) 電腦名稱。 您應該使用 Environment.MachineName 屬性來取得正確的電腦名稱。