This shows you the differences between two versions of the page.
— |
blog:2025-07-03-001 [2025/07/03 14:57] (current) jethro created |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~NOTOC~~ | ||
+ | ====== 2025-07-03 C#: 獲取當前進程的 ID ====== | ||
+ | * 在 C# 中,可以使用 System.Diagnostics 命名空間來獲取應用程序的進程 ID | ||
+ | * 以下是一些常見的方法來獲取當前應用程序的進程 ID。 | ||
+ | ===== 獲取當前進程的 ID ===== | ||
+ | * 可以使用 Process.GetCurrentProcess() 方法來獲取當前進程的相關信息,包括進程 ID。 | ||
+ | * <sxh c#>using System; | ||
+ | using System.Diagnostics; | ||
+ | |||
+ | class Program | ||
+ | { | ||
+ | static void Main() | ||
+ | { | ||
+ | // 獲取當前進程 | ||
+ | Process currentProcess = Process.GetCurrentProcess(); | ||
+ | | ||
+ | // 獲取進程 ID | ||
+ | int processId = currentProcess.Id; | ||
+ | | ||
+ | Console.WriteLine("當前進程 ID: " + processId); | ||
+ | } | ||
+ | }</sxh> | ||
+ | ===== 獲取其他進程的 ID ===== | ||
+ | * 如果想獲取其他進程的 ID,可以使用 Process.GetProcesses() 方法來列出所有正在運行的進程,然後根據進程名稱或其他屬性來篩選。 | ||
+ | * <sxh c#>using System; | ||
+ | using System.Diagnostics; | ||
+ | |||
+ | class Program | ||
+ | { | ||
+ | static void Main() | ||
+ | { | ||
+ | // 獲取所有進程 | ||
+ | Process[] processes = Process.GetProcesses(); | ||
+ | |||
+ | foreach (Process process in processes) | ||
+ | { | ||
+ | // 篩選特定的進程,例如 "notepad" | ||
+ | if (process.ProcessName.Equals("notepad", StringComparison.OrdinalIgnoreCase)) | ||
+ | { | ||
+ | Console.WriteLine("進程名稱: " + process.ProcessName + ", 進程 ID: " + process.Id); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | }</sxh> | ||
+ | ===== 總結 ===== | ||
+ | * 使用 Process.GetCurrentProcess().Id 獲取當前應用程序的進程 ID | ||
+ | * 使用 Process.GetProcesses() 方法來獲取其他進程的 ID,並通過篩選進程名稱來找到特定的進程 | ||
+ | * 這些方法可以幫助在 C# 應用程序中獲取進程 ID | ||
+ | ===== TAGS ===== | ||
+ | * | ||
+ | ====== ====== | ||
+ | * {{counter}} person(s) visited this page until now. | ||
+ | * [[:memo:index|Back]] | ||
+ | ====== ====== | ||
+ | <html><!-- | ||
+ | PDF for A4-Portrait: {{pdfjs 50%,450px > xxx.pdf?page-fit}} | ||
+ | PDF for A4-Landscape: {{pdfjs 500px,700px > xxx.pdf?page-fit}} | ||
+ | PDF for iPad Note: {{pdfjs 700px,500px > xxx.pdf?page-fit}} | ||
+ | |||
+ | Youtube: {{youtube>large:XXXXX}} | ||
+ | Code Highlight: <sxh php; first-line: 70; highlight: [89,92]; title: New title attribute in action> | ||
+ | |||
+ | --></html> | ||