How to display the list of processes specifically running Application programs in C# Console Application
I made a console application just like a task manager, in which the users can start, view and terminate process. Then there are options for viewing processes, View all, View Running apps, View Background processes, and System process. I already accomplished the first option which the "Viewl all" option, but I can't accomplish the other 3 options.
My codes are below...
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.