Winform调用外部exe文件的方法

本文介绍了三种在C#中启动程序并操作桌面鼠标的方法:1)使用内置Process类启动程序;2)通过win32API设置鼠标位置并模拟鼠标双击事件;3)利用shell32.dll库执行程序。这些技术常用于自动化和测试场景。

1、通过内置 Process 方式打开程序
1 Process m_Process = null;
2 m_Process = new Process();
3 m_Process.StartInfo.FileName = @“C:\test.exe”;
4 m_Process.Start();
2、通过win32 ,设置桌面鼠标位置,通过方法模拟鼠标双击事件,有点类似于那种外挂一样

复制代码
//在class下面放入以下方法
//绑定事件
[System.Runtime.InteropServices.DllImport(“user32”)]
private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

//定位
[DllImport(“User32”)]
public extern static void SetCursorPos(int x, int y);
//代码放在需要执行的地方
const int MOUSEEVENTF_LEFTDOWN = 0x0002;
const int MOUSEEVENTF_LEFTUP = 0x0004;
SetCursorPos(33, 28);

mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

3、shell32.dll 方法
复制代码
//class里面放入这段代码
[DllImport(“shell32.dll”)]
public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
//需要打开的地方插入此段代码
ShellExecute(IntPtr.Zero, new StringBuilder(“Open”), new StringBuilder(“test.exe”), new StringBuilder(“”), new StringBuilder(@“C:\文件夹名”), 1);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

观天小蚁

需要广大市民的支持哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值