Csharp/C Sharp by API/System.Diagnostics/Process
Содержание
- 1 Process.CloseMainWindow
- 2 Process.Exited
- 3 Process.GetCurrentProcess()
- 4 Process.GetProcessById
- 5 Process.Kill()
- 6 Process.Modules
- 7 Process.PeakWorkingSet64
- 8 Process.PrivateMemorySize
- 9 Process.ProcessName
- 10 Process.StandardOutput.ReadToEnd()
- 11 Process.Start
- 12 Process.Threads
- 13 Process.TotalProcessorTime
- 14 Process.VirtualMemorySize
- 15 Process.WaitForExit
- 16 Process.WorkingSet
Process.CloseMainWindow
using System;
using System.Threading;
using System.Diagnostics;
class MainClass
{
public static void Main()
{
using (Process process = Process.Start("notepad.exe", @"c:\SomeFile.txt"))
{
Console.WriteLine("Waiting 5 seconds before terminating notepad.exe.");
Thread.Sleep(5000);
Console.WriteLine("Terminating Notepad with CloseMainWindow.");
if (!process.CloseMainWindow())
{
Console.WriteLine("CloseMainWindow returned false - " + " terminating Notepad with Kill.");
process.Kill();
}
else
{
if (!process.WaitForExit(2000))
{
Console.WriteLine("CloseMainWindow failed to" + " terminate - terminating Notepad with Kill.");
process.Kill();
}
}
}
}
}
Process.Exited
using System;
using System.Diagnostics;
public class DetectingProcessCompletion
{
static void ProcessDone(object sender, EventArgs e)
{
Console.WriteLine("Process Exited");
}
public static void Main()
{
Process p = new Process();
p.StartInfo.FileName = "notepad.exe";
p.StartInfo.Arguments = "process3.cs";
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(ProcessDone);
p.Start();
p.WaitForExit();
Console.WriteLine("Back from WaitForExit()");
}
}
Process.GetCurrentProcess()
using System;
using System.Diagnostics;
using System.IO;
class TestPathApp {
static void Main(string[] args) {
Process p = Process.GetCurrentProcess();
ProcessModule pm = p.MainModule;
string s = pm.ModuleName;
Console.WriteLine(Path.GetFullPath(s));
Console.WriteLine(Path.GetFileName(s));
Console.WriteLine(Path.GetFileNameWithoutExtension(s));
Console.WriteLine(Path.GetDirectoryName(Directory.GetCurrentDirectory()));
Console.WriteLine(Path.GetPathRoot(Directory.GetCurrentDirectory()));
Console.WriteLine(Path.GetTempPath());
Console.WriteLine(Path.GetTempFileName());
}
}
Process.GetProcessById
using System;
using System.Diagnostics;
class MainClass
{
public static void EnumThreadsForPid(int pID)
{
Process theProc;
try {
theProc = Process.GetProcessById(pID);
} catch {
Console.WriteLine("-> Sorry...bad PID!");
return;
}
Console.WriteLine("Here are the thread IDs for: {0}", theProc.ProcessName);
ProcessThreadCollection theThreads = theProc.Threads;
foreach(ProcessThread pt in theThreads)
{
string info = string.Format("-> Thread ID: {0}\tStart Time {1}\tPriority {2}", pt.Id , pt.StartTime.ToShortTimeString(), pt.PriorityLevel);
Console.WriteLine(info);
}
}
static void Main(string[] args)
{
int theProcID = 10001;
EnumThreadsForPid(theProcID);
}
}
Process.Kill()
using System;
using System.Diagnostics;
class MainClass
{
static void Main(string[] args)
{
Console.Write("--> Hit a key to launch IE");
Console.ReadLine();
// Launch IE.
Process ieProc = Process.Start("IExplore.exe", "www.nfex.ru");
Console.Write("--> Hit a key to kill {0}...", ieProc.ProcessName);
Console.ReadLine();
try
{
ieProc.Kill();
}
catch{} // In case user already killed it...
}
}
Process.Modules
using System;
using System.Diagnostics;
class MainClass
{
public static void EnumModsForPid(int pID)
{
Process theProc;
try {
theProc = Process.GetProcessById(pID);
} catch {
Console.WriteLine("-> Sorry...bad PID!");
return;
}
Console.WriteLine("Here are the loaded modules for: {0}", theProc.ProcessName);
try
{
ProcessModuleCollection theMods = theProc.Modules;
foreach(ProcessModule pm in theMods)
{
string info = string.Format("-> Mod Name: {0}", pm.ModuleName);
Console.WriteLine(info);
}
}
catch{Console.WriteLine("No mods!");}
}
static void Main(string[] args)
{
int theProcID = 10001;
EnumModsForPid(theProcID);
}
}
Process.PeakWorkingSet64
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime;
using System.Runtime.rupilerServices;
using System.Security;
public class MainClass
{
public static void Main()
{
Process[] allProcs = Process.GetProcesses("RemoteMachineOnYourNerwork");
foreach (Process p in allProcs)
Console.WriteLine(" -> {0} - {1}", p.ProcessName, p.PeakWorkingSet64);
}
}
Process.PrivateMemorySize
using System;
using System.Diagnostics;
class MainClass
{
public static void Main()
{
Process thisProc = Process.GetCurrentProcess();
Console.WriteLine("ProcessName:"+ thisProc.ProcessName);
Console.WriteLine("Process: {0}, ID: {1}", thisProc.StartTime, thisProc.Id);
Console.WriteLine(" CPU time: {0}", thisProc.TotalProcessorTime);
Console.WriteLine(" priority class: {0} priority: {1}", thisProc.PriorityClass, thisProc.BasePriority);
Console.WriteLine(" virtual memory: {0}", thisProc.VirtualMemorySize);
Console.WriteLine(" private memory: {0}", thisProc.PrivateMemorySize);
Console.WriteLine(" physical memory: {0}", thisProc.WorkingSet);
}
}
Process.ProcessName
using System;
using System.Diagnostics;
class MainClass
{
static void Main(string[] args)
{
Process[] runningProcs = Process.GetProcesses(".");
foreach(Process p in runningProcs)
{
string info = string.Format("-> PID: {0}\tName: {1}",p.Id, p.ProcessName);
Console.WriteLine(info);
}
}
}
Process.StandardOutput.ReadToEnd()
using System;
using System.Diagnostics;
public class RedirectingProcessOutput
{
public static void Main()
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c dir *.cs";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
Console.WriteLine("Output:");
Console.WriteLine(output);
}
}
Process.Start
using System;
using System.Diagnostics;
class MainClass {
public static void Main() {
Process newProc = Process.Start("wordpad.exe");
Console.WriteLine("New process started.");
newProc.WaitForExit();
newProc.Close(); // free resources
Console.WriteLine("New process ended.");
}
}
Process.Threads
using System;
using System.Diagnostics;
class MainClass
{
public static void Main()
{
Process thisProc = Process.GetCurrentProcess();
ProcessThreadCollection myThreads = thisProc.Threads;
foreach(ProcessThread pt in myThreads)
{
Console.WriteLine("thread: {0}", pt.Id);
Console.WriteLine(" started: {0}", pt.StartTime);
Console.WriteLine(" CPU time: {0}", pt.TotalProcessorTime);
Console.WriteLine(" priority: {0}", pt.BasePriority);
Console.WriteLine(" thread state: {0}", pt.ThreadState);
}
}
}
Process.TotalProcessorTime
using System;
using System.Diagnostics;
public class GetProc
{
public static void Main()
{
Process thisProc = Process.GetCurrentProcess();
string procName = thisProc.ProcessName;
DateTime started = thisProc.StartTime;
int procID = thisProc.Id;
int memory = thisProc.VirtualMemorySize;
int priMemory = thisProc.PrivateMemorySize;
int physMemory = thisProc.WorkingSet;
int priority = thisProc.BasePriority;
ProcessPriorityClass priClass = thisProc.PriorityClass;
TimeSpan cpuTime = thisProc.TotalProcessorTime;
Console.WriteLine("Process: {0}, ID: {1}", procName, procID);
Console.WriteLine(" started: {0}", started.ToString());
Console.WriteLine(" CPU time: {0}", cpuTime.ToString());
Console.WriteLine(" priority class: {0} priority: {1}", priClass, priority);
Console.WriteLine(" virtual memory: {0}", memory);
Console.WriteLine(" private memory: {0}", priMemory);
Console.WriteLine(" physical memory: {0}", physMemory);
Console.WriteLine("\n trying to change priority...");
thisProc.PriorityClass = ProcessPriorityClass.High;
priClass = thisProc.PriorityClass;
Console.WriteLine(" new priority class: {0}", priClass);
}
}
Process.VirtualMemorySize
using System;
using System.Diagnostics;
class MainClass
{
public static void Main()
{
Process thisProc = Process.GetCurrentProcess();
Console.WriteLine("ProcessName:"+ thisProc.ProcessName);
Console.WriteLine("Process: {0}, ID: {1}", thisProc.StartTime, thisProc.Id);
Console.WriteLine(" CPU time: {0}", thisProc.TotalProcessorTime);
Console.WriteLine(" priority class: {0} priority: {1}", thisProc.PriorityClass, thisProc.BasePriority);
Console.WriteLine(" virtual memory: {0}", thisProc.VirtualMemorySize);
Console.WriteLine(" private memory: {0}", thisProc.PrivateMemorySize);
Console.WriteLine(" physical memory: {0}", thisProc.WorkingSet);
}
}
Process.WaitForExit
using System;
using System.Threading;
using System.Diagnostics;
class MainClass
{
public static void Main()
{
using (Process process = Process.Start("notepad.exe", @"c:\SomeFile.txt"))
{
Console.WriteLine("Waiting 5 seconds before terminating notepad.exe.");
Thread.Sleep(5000);
Console.WriteLine("Terminating Notepad with CloseMainWindow.");
if (!process.CloseMainWindow())
{
Console.WriteLine("CloseMainWindow returned false - " + " terminating Notepad with Kill.");
process.Kill();
}
else
{
if (!process.WaitForExit(2000))
{
Console.WriteLine("CloseMainWindow failed to" + " terminate - terminating Notepad with Kill.");
process.Kill();
}
}
}
}
}
Process.WorkingSet
using System;
using System.Diagnostics;
class MainClass
{
public static void Main()
{
Process thisProc = Process.GetCurrentProcess();
Console.WriteLine("ProcessName:"+ thisProc.ProcessName);
Console.WriteLine("Process: {0}, ID: {1}", thisProc.StartTime, thisProc.Id);
Console.WriteLine(" CPU time: {0}", thisProc.TotalProcessorTime);
Console.WriteLine(" priority class: {0} priority: {1}", thisProc.PriorityClass, thisProc.BasePriority);
Console.WriteLine(" virtual memory: {0}", thisProc.VirtualMemorySize);
Console.WriteLine(" private memory: {0}", thisProc.PrivateMemorySize);
Console.WriteLine(" physical memory: {0}", thisProc.WorkingSet);
}
}