C# 通过进程名/进程Id 操作窗口
2021-03-26 23:28
标签:param inf 接收 htm enum file user tcl dma 根据进程Id关闭应用窗口: 关闭所有此进程名的窗口: 当然,直接杀进程,是最快的方法: 窗口之间发送/接收消息的处理,请参考《C# 跨进程通信》 C# 通过进程名/进程Id 操作窗口 标签:param inf 接收 htm enum file user tcl dma 原文地址:https://www.cnblogs.com/kybs0/p/9369159.html1. 判断窗口是否存在
1 private bool IsWindowExist(IntPtr handle)
2 {
3 return (!(GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero) && IsWindowVisible(new HandleRef(this, handle)));
4 }
5
6 [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
7 public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd);
8
9 [DllImport("user32.dll", CharSet = CharSet.Auto)]
10 public static extern bool IsWindowVisible(HandleRef hWnd);
2. 获取窗口句柄
1 ///
3. 关闭应用窗口
1 ///
1 public void CloseAllWindows(string processName)
2 {
3 Process[] processList = Process.GetProcessesByName(processName);
4 foreach (Process process in processList)
5 {
6 CloseMainWindow(process.Id);
7 }
8 }
1 ///
4. 重启程序
1 string appFileName = currentClientProcess.MainModule.FileName;
2
3 Process newClient = new Process();
4 newClient.StartInfo.FileName = appFileName;
5 // 设置要启动的进程的初始目录
6 newClient.StartInfo.WorkingDirectory = System.Windows.Forms.Application.ExecutablePath;
7
8 //启动程序
9 currentClientProcess.Kill();
10 newClient.Start();