我只是试图在使用C#的Windows 7机器上执行shell命令。使用自定义命令执行shell
我发现这个片段在网上:
[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],这对于这个问题应该是完美的,但不幸的是并不支持所有的上下文菜单条目)。
UPDATE
我才发现我的问题的解决方案使用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 http://www.autohotkey.com/或AutoIt http://www.autoitscript.com/site/autoit/ – rdrmntn