C# 关联启动外部程序,监听程序是否运行

启动外部程序

string dirpath = Path.Combine(config.PicSavePath, DateTime.Now.ToString("yyyyMMdd"));
            if (!Directory.Exists(dirpath))
                Directory.CreateDirectory(dirpath);
                //关联启动  
            System.Diagnostics.Process.Start("D:\\PicInput\\PicInput.exe");

监听程序是否运行

 //监听当前程序进程
           /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //监听当前程序进程
             string Name = Process.GetCurrentProcess().ProcessName;
            Process[] forms= Process.GetProcessesByName(Name);
            //判断程序进程是否已开启
            if (forms.Length > 1)
            {
                //若开启,则关闭新实例
              Application.Exit();
              return;
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
取消外部程序的鼠标监听事件需要使用 Windows API 来实现。具体步骤如下: 1. 首先,需要获取目标程序的进程句柄,可以使用 Process 类来获取。 2. 使用 Windows API 函数 SetWindowsHookEx 来安装一个鼠标钩子,并获取该钩子的句柄。 3. 使用 Windows API 函数 UnhookWindowsHookEx 来卸载该钩子,从而取消鼠标监听事件。 下面是 C# 代码示例: ``` using System; using System.Diagnostics; using System.Runtime.InteropServices; public class MouseHook { private const int WH_MOUSE_LL = 14; private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam); private static IntPtr hookID = IntPtr.Zero; [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetModuleHandle(string lpModuleName); private static IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam) { return IntPtr.Zero; } public static void UninstallHook(Process targetProcess) { if (hookID != IntPtr.Zero) { UnhookWindowsHookEx(hookID); hookID = IntPtr.Zero; } } public static void InstallHook(Process targetProcess) { hookID = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProc, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0); } } ``` 在上述代码示例中,InstallHook 方法用于安装鼠标钩子,UninstallHook 方法用于卸载鼠标钩子。调用 InstallHook 方法后,该钩子会监听所有的鼠标事件,直到调用 UninstallHook 方法将其卸载。在实际使用时,可以在需要取消鼠标监听事件的地方调用 UninstallHook 方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值