簡體   English   中英

如何在 C# 控制台應用程序中顯示專門運行應用程序的進程列表

[英]How to display the list of processes especifically running Application programs in C# Console Application

如何在C#控制台應用程序中顯示專門運行應用程序的進程列表

我制作了一個控制台應用程序,就像一個任務管理器,用戶可以在其中啟動、查看和終止進程。 然后有查看進程、查看全部、查看正在運行的應用程序、查看后台進程和系統進程的選項。 我已經完成了“查看全部”選項的第一個選項,但我無法完成其他 3 個選項。

我的代碼在下面...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace taskManager
{
    class Program
    {
        static void Main(string[] args)
        {
            string a;
            intro:
            Console.WriteLine("OPTIONS:");
            Console.WriteLine("1. Start Process");
            Console.WriteLine("2. View Process");
            Console.WriteLine("3. Terminate a Process");
            Console.Write("Enter Option:");
            a = Console.ReadLine();
            if (a == "1") goto start;
            if (a == "2") goto view;
            if (a == "3") goto terminate;
            Console.WriteLine("Invalid option!");
            goto intro;
            start:
            { 
                Console.WriteLine("Enter Process name:");
                a = Console.ReadLine();
                Process.Start(a);
                goto intro;
            }
            view:
            { 
                Console.WriteLine("OPTIONS for View:");
                Console.WriteLine("a. View all");
                Console.WriteLine("b. View Applications");
                Console.WriteLine("c. View Background process");
                Console.WriteLine("d. View System Process");
                Console.Write("Enter Option:");
                a=Console.ReadLine();
                if (a == "a") goto a;
                if (a == "b") goto b;
                a:
                {
                    Console.WriteLine("Processes");
                    Process[] processes = Process.GetProcesses();
                    Console.WriteLine("No. Of Pocess: {0}", processes.Length);
                    foreach (Process process in processes)
                    {
                        Console.WriteLine(process.ProcessName);
                    }
                    goto intro;
                }
    //This is where the unaccomplished options
        b:Apps
        c:background Process
        d:System process
            }
            terminate:
            {
                Console.WriteLine("Enter process name:");
                a = Console.ReadLine();
                foreach (var process in Process.GetProcessesByName(a))
                {
                    process.Kill();

                }
                Console.WriteLine("Process terminated!");
                goto intro;
            }
        }
    }
}
    public static void ListAllProcess()
    {
        int processCount = 0;
        Process[] processList = Process.GetProcesses();

        // view All running process
        foreach (Process process in processList)
        {
            Console.WriteLine("Process: {0} <> ID: {1}", process.ProcessName, process.Id);
            processCount += 1;
        }
        Console.WriteLine("Number of Total Process: {0} ", processCount);
    }

    static void Main(string[] args)
    {

        // view All running process..
        ListAllProcess();

        try
        {
            // for Start a new Process
            Console.Write("\n\nDrag-Drop or enter a full path of a executable file: ");
            string applicationPath = Console.ReadLine();
            Process.Start(applicationPath);

            Console.WriteLine("'{0}' is started.", applicationPath);

            // This start program itself. Don't try this...
            // string appPath = System.Reflection.Assembly.GetEntryAssembly().Location;
            // System.Diagnostics.Process.Start(appPath);
            Console.Write("\n\n\n");
            ListAllProcess();

        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred: '{0}'", ex);
        }


        try
        {
            // for kill a running Process
            Console.Write("\n\nEnter a Process ID:");
            int processID = Convert.ToInt32(Console.ReadLine());
            Process.GetProcessById(processID).Kill();
            Console.WriteLine("Process '{0}' died.", processID);
            Console.Write("\n\n\n");
            ListAllProcess();
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred: '{0}'", ex);
        }

        Console.ReadLine();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM