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 则输出该 ...
随机推荐
- 整理SpringMVC
Spring Web MVC核心架构图: 核心架构图流程如下: 1.首先用户发送请求------->DispatcherServlet(前端控制器),前端控制器收到请求后自己不进行处理,而是委托 ...
- 到位App_jQuery_art-template
到位 App 不写 node 服务器,本地模拟 ajax 获取 json 数据 源代码 ---- 参见 ---- 使用 webstorm 运行 index.html 本地静态的 data.json 前 ...
- [LeetCode] Lemonade Change 买柠檬找零
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...
- SpringAop注解实现日志的存储
一.介绍 1.AOP的作用 在OOP中,正是这种分散在各处且与对象核心功能无关的代码(横切代码)的存在,使得模块复用难度增加.AOP则将封装好的对象剖开,找出其中对多个对象产生影响的公共行为,并将其封 ...
- K8S 安装 Wordpress
基本概念 Helm 可以理解为 Kubernetes 的包管理工具,可以方便地发现.共享和使用为Kubernetes构建的应用,它包含几个基本概念 Helm是目前Kubernetes服务编排领域的唯一 ...
- mysql中Table is read only的解决方法
首先去到mysq的bin目录 cd /usr/local/mysql/bin 执行如下mysqladmin ./mysqladmin -p flush-tables 接着输入数据库存的root密码即可
- spring的定时器
一:基于xml配置的方式 1:编写普通的pojo 类 package com.aflyun.web.task; import org.springframework.stereotype.Compon ...
- iOS URL Schemes与漏洞的碰撞组合
iOS URL Schemes与漏洞的碰撞组合 前言 iOS URL Schemes,这个单词对于大多数人来说可能有些陌生,但是类似下面这张图的提示大部分人应该都经常看见: 今天要探究的就是:了解iO ...
- 执行find / -name *.sh时报错 find: 路径必须在表达式之前: start-ressvr-release.sh
想查找一个包含4000多文件的目录下所有.sh结尾的文件 使用命令 find ./ -name *.sh (本身已经在要查找的目录里了) 结果报错: 解决方法一:find ./ - ...
- MySQL忘记密码后找回密码
停止mysql服务 在mysql配置文件里[mysqld]下添加skip-grant-tables 3.重启服务,进入mysql 4.修改密码: MariaDB [mysql]> update ...