c#使用ShellExecute
c#使用ShellExecute | Id | Title | DateAdded | SourceUrl | PostType | Body | BlogId | Description | DateUpdated | IsMarkdown | EntryName | CreatedTime | IsActive | AutoDesc | AccessPermission |
| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------|
| 16955559| c#使用ShellExecute | 2022-12-06T15:54:00| | BlogPost|
[DllImport("kernel32.dll")]
public static extern int WinExec(string exeName, int operType);
不推荐使用以上代码。
ShellExecute
若ShellExecute函数调用成功,则返回值为被执行程序的实例句柄。若返回值小于32,则表示出现错误。
/// <summary>
/// ShellExecute
/// </summary>
/// <param name="hwnd">指定父窗口句柄:ntPtr.Zero</param>
/// <param name="lpszOp">指定要进行的操作:Open</param>
/// <param name="lpszFile">指定要打开的文件名|路径</param>
/// <param name="lpszParams">指定命令行参数: 0 | ""</param>
/// <param name="lpszDir">用于指定默认目录:0 | ""</param>
/// <param name="FsShowCmd">显示模式: 0:隐藏 1~11</param>
/// <returns></returns>
[DllImport("shell32.dll")]
private static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
</span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span>
<span style="color: #808080;">///</span><span style="color: #008000;"> 打开程序/文件夹
</span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"><param name="path"></span><span style="color: #008000;">路径</span><span style="color: #808080;"></param></span>
<span style="color: #808080;">///</span> <span style="color: #808080;"><param name="FsShow"></span><span style="color: #008000;">显示模式 默认1</span><span style="color: #808080;"></param></span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> OpenFile(<span style="color: #0000ff;">string</span> path, <span style="color: #0000ff;">int</span> FsShow = <span style="color: #800080;">1</span><span style="color: #000000;">)
{
ShellExecute(IntPtr.Zero, </span><span style="color: #0000ff;">new</span> StringBuilder(<span style="color: #800000;">"</span><span style="color: #800000;">Open</span><span style="color: #800000;">"</span>), <span style="color: #0000ff;">new</span> StringBuilder(@path), <span style="color: #0000ff;">new</span> StringBuilder(<span style="color: #800000;">""</span>), <span style="color: #0000ff;">new</span> StringBuilder(<span style="color: #800000;">""</span><span style="color: #000000;">), FsShow);
}</span></pre>
| 648658| 磁力搜索| 2022-12-06T16:09:00| false| | 2022-12-06T15:53:50.07| true| [DllImport("kernel32.dll")] public static extern int WinExec(string exeName, int operType); 不推荐使用以上代码。 ShellExecute若ShellExecute函数调用成功,则返回值为被执行程序的实例句柄| Anonymous|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义