[英]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.