本文整理匯總了C#中System.Diagnostics.Process.Id屬性的典型用法代碼示例。如果您正苦於以下問題:C# Process.Id屬性的具體用法?C# Process.Id怎麽用?C# Process.Id使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類System.Diagnostics.Process
的用法示例。
在下文中一共展示了Process.Id屬性的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.Threading;
using System.Security.Permissions;
using System.Security.Principal;
using System.Diagnostics;
class ProcessDemo
{
public static void Main()
{
Process notePad = Process.Start("notepad");
Console.WriteLine("Started notepad process Id = " + notePad.Id);
Console.WriteLine("All instances of notepad:");
// Get Process objects for all running instances on notepad.
Process[] localByName = Process.GetProcessesByName("notepad");
int i = localByName.Length;
while (i > 0)
{
// You can use the process Id to pass to other applications or to
// reference that particular instance of the application.
Console.WriteLine(localByName[i - 1].Id.ToString());
i -= 1;
}
i = localByName.Length;
while (i > 0)
{
Console.WriteLine("Enter a process Id to kill the process");
string id = Console.ReadLine();
if (string.IsNullOrEmpty(id))
break;
try
{
using (Process chosen = Process.GetProcessById(Int32.Parse(id)))
{
if (chosen.ProcessName == "notepad")
{
chosen.Kill();
chosen.WaitForExit();
}
}
}
catch (Exception e)
{
Console.WriteLine("Incorrect entry.");
continue;
}
i -= 1;
}
}
}