C# 通过进程名/进程Id 操作窗口/程序
1. 判断窗口是否存在
private bool IsWindowExist(IntPtr handle)
{
return (!(GetWindow(new HandleRef(this, handle), ) != IntPtr.Zero) && IsWindowVisible(new HandleRef(this, handle)));
} [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd); [DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool IsWindowVisible(HandleRef hWnd);
2. 获取窗口句柄
/// <summary>
/// 获取应用程序窗口句柄
/// </summary>
/// <param name="processId"></param>
/// <returns></returns>
private IntPtr GetWindowHandle(int processId)
{
var windowHandle = IntPtr.Zero;
EnumThreadWindowsCallback windowsCallback = new EnumThreadWindowsCallback(FindMainWindow);
EnumWindows(windowsCallback, IntPtr.Zero);
//保持循环
GC.KeepAlive(windowsCallback); bool FindMainWindow(IntPtr handle, IntPtr extraParameter)
{
int num;
GetWindowThreadProcessId(new HandleRef(this, handle), out num);
if (num == processId && IsWindowExist(handle))
{
windowHandle = handle;
return true;
}
return false;
} return windowHandle;
}
public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);
3. 关闭应用窗口
根据进程Id关闭应用窗口:
/// <summary>
/// 关闭程序主窗口
/// </summary>
/// <param name="processId">进程ID</param>
public void CloseWindow(int processId)
{
var mwh = GetWindowHandle(processId);
SendMessage(mwh, MW_CLOSE, , );
}
const int MW_CLOSE = 0x0010; [DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
关闭所有此进程名的窗口:
public void CloseAllWindows(string processName)
{
Process[] processList = Process.GetProcessesByName(processName);
foreach (Process process in processList)
{
CloseMainWindow(process.Id);
}
}
当然,直接杀进程,是最快的方法:
/// <summary>
/// 关闭所有进程
/// </summary>
/// <param name="processName"></param>
/// <returns></returns>
public static bool CloseAllProcesses(string processName)
{
try
{
Process[] psEaiNotes = Process.GetProcessesByName(processName);
foreach (Process psEaiNote in psEaiNotes)
{
psEaiNote.Kill();
}
}
catch
{
return false;
} return true;
}
4. 重启程序
string appFileName = currentClientProcess.MainModule.FileName;
Process newClient = new Process();
newClient.StartInfo.FileName = appFileName;
// 设置要启动的进程的初始目录
newClient.StartInfo.WorkingDirectory = System.Windows.Forms.Application.ExecutablePath;
//启动程序
currentClientProcess.Kill();
newClient.Start();
窗口之间发送/接收消息的处理,请参考《C# 跨进程通信》
C# 通过进程名/进程Id 操作窗口/程序的更多相关文章
- 根据进程ID查找运行程序目录
查看进程ID [root@hadoop03 openresty]# netstat -nltp 进入/proc目录查找相应进程ID目录并进入此目录 [root@hadoop03 usr]# cd /p ...
- QT_获取正在运行程序的进程id(判断程序是否正在运行)
bool checkProcessRunning(const QString &processName, QList<quint64> &listProcessId) { ...
- C++ Windows 下 根据进程名获取进程ID 以及该进程下所有窗口的句柄
#include <windows.h> #include <stdint.h> #include <tlhelp32.h> #include <stdio. ...
- 调用window Api 进行对特定窗口的进程ID进行操作
/// <summary> /// 获取窗口句柄 /// </summary> /// <param name="lpClassName">&l ...
- 显示所有APP的进程详细信息(进程ID、进程所在UID、进程占用内存、进程名)
main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...
- C# 最基本的涉及模式(单例模式) C#种死锁:事务(进程 ID 112)与另一个进程被死锁在 锁 | 通信缓冲区 资源上,并且已被选作死锁牺牲品。请重新运行该事务,解决方案: C#关闭应用程序时如何关闭子线程 C#中 ThreadStart和ParameterizedThreadStart区别
C# 最基本的涉及模式(单例模式) //密封,保证不能继承 public sealed class Xiaohouye { //私有的构造函数,保证外部不能实例化 private ...
- C#获取运行程序的进程ID
C#获取运行程序的进程ID [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int G ...
- 如何通过进程名获取进程ID
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何通过进程名获取进程ID.
- Linux/UNIX编程:获取指定用户所有正在运行的进程ID和进程名
先用系统函数 `getpwnam` 获得指定用户名的 UID,然后遍历 /proc/ 中所有 PID 目录,如果 /proc/PID/status 中的 UID 是输入用户名对应的 UID 则输出该 ...
随机推荐
- [LeetCode] Minimum Cost to Merge Stones 混合石子的最小花费
There are N piles of stones arranged in a row. The i-th pile has stones[i] stones. A move consists ...
- MangoDb的安装及使用
安装步骤 一.创建文件 vi /etc/yum.repos.d/mongodb-org-3.6.repo 二.配置文件内容 [mongodb-org-3.6] name=MongoDB Reposit ...
- Httpclient代码
/// <summary> /// 显示 /// </summary> /// <returns></returns> public ActionRes ...
- 升讯威微信营销系统开发实践:微信接口的 .NET 封装
GitHub:https://github.com/iccb1013/Sheng.WeixinConstruction因为个人精力时间有限,不会再对现有代码进行更新维护,不过微信接口比较稳定,经测试至 ...
- 使用RSA加密在Python中逆向shell
i春秋翻译小组-Neo(李皓伟) 使用RSA加密在Python中逆向shell 这是一个关于使用RSA加密编程逆向shell的python教程. 我想提一下,这篇文章更多的是关于理解shell中涉及的 ...
- FFmpeg 结构体学习(四): AVFrame 分析
在上文FFmpeg 结构体学习(三): AVPacket 分析我们学习了AVPacket结构体的相关内容.本文,我们将讲述一下AVFrame. AVFrame是包含码流参数较多的结构体.下面我们来分析 ...
- ubuntu系统界面改变
主题:https://gitzab.com/Anduin/GNOME-OSX-II-Theme.git图标:https://github.com/keeferrourke/la-capitaine-i ...
- [Swift]LeetCode476. 数字的补数 | Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- SpringMVC+JWT+Swagger UI+RestFul
前言: 其实很早就想写这篇文章了,因为我觉得这会对很多新手有指引作用,当初自己也是瞎子过河的摸索着过来的.目前后台开发比较流行的MVC框架中使用Spring MVC还是比较多的,当然还有Spring ...
- 网络协议 2 - IP 是怎么来,又是怎么没的?
了解完网络协议,我们会发现,网络通信的五层模型里,有两个很重要的概念:IP 地址和 MAC 地址. 那么 IP 地址是怎么来的,又是怎么没的?MAC 地址与 IP 地址又有什么区别? 这回答上面问题前 ...