【发布时间】:2014-01-08 09:53:56
【问题描述】:
我只是想在 Windows 7 机器上使用 C# 执行 shell 命令。
我在网上找到了这个 sn-p:
[DllImport("shell32.dll", EntryPoint = "ShellExecute")]
public static extern long ShellExecute(int hwnd, string cmd, string file, string param1, string param2, int swmode);
void exe()
{
ShellExecute (0, "open", "C:\WINDOWS\Zapotec.bmp", "", "", 5)
}
这很好用,但我不想打开它,我想执行一个自定义命令(来自第三方应用程序)。具体:我想使用没有命令行界面的病毒扫描程序进行病毒扫描(可以在 Windows 资源管理器的上下文菜单中启动)。
有什么建议吗?我没有在互联网上找到任何有用的信息。 它不必是 C# 解决方案。它也可以是一个外部文件(我发现程序“runmenu”[http://www.programbits.co.uk/downloads/runmenu.zip],它应该可以完美解决这个问题,但不幸的是并非所有上下文菜单项都受支持)。
更新
我刚刚使用 powershell 找到了解决问题的方法:
PS C:\temp> $o = new-object -com Shell.Application
PS C:\temp> $folder = $o.NameSpace("C:\temp")
PS C:\temp> $file=$folder.ParseName("test.txt")
PS C:\temp> $file.Verbs() | select Name
PS C:\temp> $file.Verbs() | %{ if($_.Name -eq 'Edit with &Notepad++') { $_.DoIt() } }
【问题讨论】:
-
有点不清楚你想做什么。你不能只使用一些 gui 自动化脚本,如 AutoHotKey autohotkey.com 或 AutoIt autoitscript.com/site/autoit
标签: c# windows shell contextmenu explorer