Export (0) Print
Expand All

Process.MachineName Property

Gets the name of the computer the associated process is running on.

[Visual Basic]
Public ReadOnly Property MachineName As String
[C#]
public string MachineName {get;}
[C++]
public: __property String* get_MachineName();
[JScript]
public function get MachineName() : String;

Property Value

The name of the computer that the associated process is running on.

Exceptions

Exception Type Condition
InvalidOperationException There is no process associated with this Process object.

Remarks

You can view statistical data and process information for processes running on remote computers but you cannot call Start, CloseMainWindow, or Kill on remote computers.

Example

[Visual Basic, C#, C++] The following example starts Notepad on a remote machine specified by the user in the console. It then retrieves all instances of Notepad running on that remote machine, and displays their respective ProcessName, Id and MachineName.

[Visual Basic] 
Imports System
Imports System.Diagnostics
Imports Microsoft.VisualBasic

Class GetProcessesByNameClass

   'Entry point which delegates to C-style main Private Function
   Public Overloads Shared Sub Main()
      Main(System.Environment.GetCommandLineArgs())
   End Sub

   Public Overloads Shared Sub Main(ByVal args() As String)
      Try

         Console.Writeline("Create notepad processes on remote computer")
         Console.Write("Enter remote computer name : ")
         Dim remoteMachineName As String = Console.ReadLine()
         ' 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 SystemException
         Console.Write("Caught Exception .... : " & e.Message)
      Catch e As Exception
         Console.Write("Caught Exception .... : " & e.Message)
      End Try
   End Sub 'Main
End Class 'GetProcessesByNameClass

[C#] 
using System;
using System.Diagnostics;

class GetProcessesByNameClass
{
   public static void Main(string[] args)
   {
      try
      {

         Console.Write("Create notepad processes on remote computer \n");
         Console.Write("Enter remote computer name : ");
         string remoteMachineName = Console.ReadLine();
         // 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.Write("Process Name : " + myProcess.ProcessName + "  Process ID : "
               + myProcess.Id + "  MachineName : " + myProcess.MachineName + "\n");
         }

      }
      catch(SystemException e)
      {
         Console.Write("Caught Exception .... : " + e.Message);
      }
      catch(Exception e)
      {
         Console.Write("Caught Exception .... : " + e.Message);
      }
   }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Diagnostics;

int main() {
    try {
        Console::Write(S"Create notepad processes on remote computer \n");
        Console::Write(S"Enter remote computer name : ");
        String* remoteMachineName = Console::ReadLine();
        // Get all notepad processess into Process array.
        Process* myProcesses[] = Process::GetProcessesByName(S"notepad",remoteMachineName);
        if(myProcesses->Length == 0)
            Console::WriteLine(S"Could not find notepad processes on remote computer.");
        Collections::IEnumerator* myEnum = myProcesses->GetEnumerator();
        while (myEnum->MoveNext()) {
            Process* myProcess = __try_cast<Process*>(myEnum->Current);

            Console::Write(S"Process Name : {0}  Process ID : {1}  MachineName : {2}\n",
                myProcess->ProcessName, __box(myProcess->Id), myProcess->MachineName);
        }
    } catch (SystemException* e) {
        Console::Write(S"Caught Exception .... : {0}", e->Message);
    } catch (Exception* e) {
        Console::Write(S"Caught Exception .... : {0}", e->Message);
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

Process Class | Process Members | System.Diagnostics Namespace | GetProcesses | GetProcessById | GetProcessesByName

Show:
© 2014 Microsoft