2014-01-08 122 views
0

我只是试图在使用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() } } 

enter link description here

+0

它有点不清楚你想要做什么。难道你不能只使用一些gui自动化脚本如AutoHotKey http://www.autohotkey.com/或AutoIt http://www.autoitscript.com/site/autoit/ – rdrmntn

回答

0

难道你不使用的Process.Start()?

您可以使用ProcessStartInfo重载根据需要发送参数。

在System.Diagnostics名称空间中找到。

0

由Jason推荐,
查看.NET Framework的System.Diagnostics命名空间。它用于调用Process。

您可以将ProcessStartInfo的useShellExecute属性设置为true;抑制窗口产生,。

0

最好使用System.Diagnostics.Process类(MSDN)。

+1

@ user2445254请不要添加新的问题到通过编辑来回答。用评论来回答作者的答案。 – SoonDead

+0

对不起,我想添加评论 - 缺乏专注;)对不起。 – user2445254

1

试试这个:

using System.Diagnostics; 

private void runCom(string command) 
{ 
    ProcessStartInfo procInfo = new ProcessStartInfo("cmd", "/c" + command); 
    startInfo.UseShellExecute = false; 
    startInfo.CreateNoWindow = true; 
    Process proc = new Process(); 
    proc.StartInfo = startInfo; 
    proc.Start(); 
    proc.WaitForExit(); 
} 
+0

感谢您的回复。但我怎么才能执行一个shell命令(在Windows资源管理器中的上下文菜单项)。你的代码只运行带有参数的可执行文件? – user2445254