C# 小技巧

public static void MouseMove(Point CurrentPoint, Point TargetPoint)//鼠标带轨迹移动
{
//当前坐标 CurrentPoint
//目标位置坐标 TargetPoint
int d;
int d1;
int d2;
int x;
int y;
//获取当前坐标
GetCursorPos(ref CurrentPoint);
if (CurrentPoint.X < 0 || CurrentPoint.Y < 0) return;
d1 = TargetPoint.X - CurrentPoint.X;
d2 = TargetPoint.Y - CurrentPoint.Y;
d = (Math.Abs(d1) > Math.Abs(d2)) ? Math.Abs(d1) : Math.Abs(d2);
if (d > 0)
{
for (int i = 0; i <= d; i++)
{
x = CurrentPoint.X + i * d1 / d;
y = CurrentPoint.Y + i * d2 / d;
SetCursorPos(x, y);
Thread.Sleep(1);
}
}
}

 //关闭某个进程

var processes = Process.GetProcessesByName("CefSharp.BrowserSubprocess");
foreach (Process p in processes)
{
try
{
p.Kill();
}
catch (Exception ex)
{
Console.WriteLine("关闭异常:{0}", ex.Message);
}
}

//通过获取PID来关闭一个进程

public static void EndProcess(int pid)
{
try
{
Process process = Process.GetProcessById(pid);
process.Kill();
MessageBox.Show("此线程关闭成功!");
}
catch
{
    //System.Diagnostics.Process.Start("shutdown", @"/r"); //计算机重启
}
}

//创建并执行一个进程

string path = System.Environment.CurrentDirectory;
Process proc = null;
try
{
proc = new Process();
proc.StartInfo.FileName = path + "\" + AppName ;
proc.StartInfo.Arguments = string.Format("10");//this is argument
proc.StartInfo.CreateNoWindow = false;
proc.Start();
}
catch (Exception ex)
{
Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
}

//获取进程表Class

public class ProcessInfo
{
public ProcessInfo(int ProcessID, string ProcessName, double ProcessorTime,
long WorkingSet, string ProcessPath)
{
this.ProcessID = ProcessID;
this.ProcessName = ProcessName;
this.ProcessorTime = ProcessorTime;
this.WorkingSet = WorkingSet;
this.ProcessPath = ProcessPath;
}

private int m_ProcessID;
public int ProcessID
{
get
{
return m_ProcessID;
}
set
{
m_ProcessID = value;
}
}

private string m_ProcessName;
public string ProcessName
{
get
{
return m_ProcessName;
}
set
{
m_ProcessName = value;
}
}

private double m_ProcessorTime;
public double ProcessorTime
{
get
{
return m_ProcessorTime;
}
set
{
m_ProcessorTime = value;
}
}

private long m_WorkingSet;
public long WorkingSet
{

get
{
return m_WorkingSet;
}
set
{
m_WorkingSet = value;
}
}

private string m_ProcessPath;
public string ProcessPath
{

get
{
return m_ProcessPath;
}
set
{
m_ProcessPath = value;
}
}
}

//引用进程表方法

public List<ProcessInfo> GetProcessInfo()
{
Console.WriteLine("sucess get process");
AppList.Clear();
List<ProcessInfo> pInfo = new List<ProcessInfo>();
Process[] processes = Process.GetProcesses();
foreach (Process instance in processes)
{
try
{
pInfo.Add(new ProcessInfo(instance.Id,
instance.ProcessName,
instance.TotalProcessorTime.TotalMilliseconds,
instance.WorkingSet64,
instance.MainModule.FileName));

listProcessName.Add(instance.ProcessName);
listProcessId.Add(instance.Id);
for(int i=0;i<listProcessId.Count;i++)
{
for (int j = 0; j < i - 1; j++)
{
int temp = 0;
if (Int32.Parse(listProcessId[j].ToString()) > Int32.Parse(listProcessId[j+1].ToString()))
{
temp = Int32.Parse(listProcessId[j].ToString());
listProcessId[j] = listProcessId[j + 1];
listProcessId[j + 1] = temp;
}
}
}
AppList.Text += instance.Id + " " + instance.ProcessName + " ";
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
return pInfo;
}

只有不断学习,才可进步。
原文地址:https://www.cnblogs.com/onlyforliu/p/5669356.html