0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

test20241028

Last updated at Posted at 2024-10-28

C#でプロセス名を指定してキルするコード

AI生成によるコードです。

using System;
using System.Diagnostics;

namespace KillProcess
{
    class Program
    {
        static void Main(string[] args)
        {
            // 終了させたいプロセス名を指定(拡張子不要、例えば "notepad")
            string processName = "プロセス名をここに入力";

            try
            {
                // 指定した名前のプロセスをすべて取得
                Process[] processes = Process.GetProcessesByName(processName);

                if (processes.Length == 0)
                {
                    Console.WriteLine($"{processName} は見つかりませんでした。");
                }
                else
                {
                    // 各プロセスをキルする
                    foreach (Process process in processes)
                    {
                        process.Kill();
                        process.WaitForExit(); // プロセスが完全に終了するまで待機
                        Console.WriteLine($"プロセス {process.ProcessName} (PID: {process.Id}) を終了しました。");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("エラーが発生しました: " + ex.Message);
            }
        }
    }
}

0
0
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?