Process.MachineName 属性

定义

获取关联进程正在其上运行的计算机的名称。

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 对象关联的进程。

示例

若要使用以下示例,必须先在远程计算机上启动至少一个记事本实例。 该示例请求运行记事本的远程计算机的名称,然后显示每个实例各自的 ProcessNameIdMachineName 属性。

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

注解

可以查看在远程计算机上运行的进程的统计数据和处理信息,但不能在远程计算机上调用 StartCloseMainWindowKill

注意

当关联进程在本地计算机上执行时,此属性返回句点 (”。计算机名称的 ) 。 应使用 Environment.MachineName 属性获取正确的计算机名称。

适用于

另请参阅