2011-09-30 160 views
8

这里是我的代码:以管理员身份运行cmd以及命令?

try 
{ 
    ProcessStartInfo procStartInfo = new ProcessStartInfo(
              "cmd.exe", 
              "/c " + command); 
    procStartInfo.UseShellExecute = true; 
    procStartInfo.CreateNoWindow = true; 
    procStartInfo.Verb = "runas"; 
    procStartInfo.Arguments = "/env /user:" + "Administrator" + " cmd" + command; 

    ///command contains the command to be executed in cmd 
    System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
    proc.StartInfo = procStartInfo; 
    proc.Start(); 
} 
catch (Exception ex) 
{ 
    MessageBox.Show(ex.Message); 
} 

我想保持

procStartInfo.UseShellExecute = true 
procStartInfo.RedirectStandardInput = false; 

是否有可能不使用process.standardinput执行命令? 我尝试执行我在参数中传递的命令,但命令不执行。

+0

**是否有可能不使用process.standardinput执行命令?我尝试执行我在参数中传递的命令,但命令没有执行。**您目前没有使用它,所以这个问题没有太大意义。请做你的用户的青睐,并让命令提示符可见,以便他们知道发生了什么。 –

+1

你不想'动词'。这并不意味着你的想法。我认为。 –

+0

@Ramhound procStartInfo.Arguments =“/ env/user:”+“Administrator”+“cmd”+ ** command **; – purvang

回答

1

你为什么用参数初始化过程对象,然后重写这些参数?和顺便说一句:最后一位你设置参数你连接'命令'直到'cmd',这没有多大意义,可能是它失败的地方(看起来像你缺少一个空间)。

此外,您目前正在使用标准命令行,您可能需要使用the runas tool来代替。你也可以从命令行调用runas。

另外,你为什么要从命令行运行'命令'?为什么不直接从Process.Start直接启动它,然后在那里提供管理权限?这里有点伪代码:

Process p = Process.Start(new ProcessStartInfo() 
{ 
    FileName = <your executable>, 
    Arguments = <any arguments>, 
    UserName = "Administrator", 
    Password = <password>, 
    UseShellExecute = false, 
    WorkingDirectory = <directory of your executable> 
}); 
+0

感谢您的想法,但我不知道如何授予管理员权限process.start.can你给一些示例代码或示例? – purvang

+0

用一些伪代码更新了答案 – mtijn

5

正如@mtijn说,你已经得到了很多事情,你以后也覆盖。你还需要确保你正确地逃避了事情。

比方说,你想运行高架下面的命令:

dir c:\ 

首先,如果你只是跑通过Process.Start()此命令窗口将弹出打开和关闭的时候了,因为没有什么保持窗口打开。它处理命令并退出。为了保持窗口打开,我们可以包装在单独的命令窗口中的命令,并使用/K开关以保持其运行:

cmd /K "dir c:\" 

要运行该命令提升我们可以使用runas.exe就像你,不同的是,我们需要转义事情多一点。根据帮助文档(runas /?),我们传递给runas的命令中的任何引号需要使用反斜线进行转义。不幸的是,用上面的命令做了这个,给了我们一个双反斜杠,让cmd解析器混淆了,因此也需要转义。所以上面的命令,都会变成:

cmd /K \"dir c:\\\" 

最后,使用你提供的,我们可以换一切成一个runas命令,并在另外一组引号括我们上面命令的语法:

runas /env /user:Administrator "cmd /K \"dir c:\\\"" 

从命令提示符运行上述命令以确保其按预期工作。

鉴于这一切最终的代码变得更容易组装:

 //Assuming that we want to run the following command: 
     //dir c:\ 

     //The command that we want to run 
     string subCommand = @"dir"; 

     //The arguments to the command that we want to run 
     string subCommandArgs = @"c:\"; 

     //I am wrapping everything in a CMD /K command so that I can see the output and so that it stays up after executing 
     //Note: arguments in the sub command need to have their backslashes escaped which is taken care of below 
     string subCommandFinal = @"cmd /K \""" + subCommand.Replace(@"\", @"\\") + " " + subCommandArgs.Replace(@"\", @"\\") + @"\"""; 

     //Run the runas command directly 
     ProcessStartInfo procStartInfo = new ProcessStartInfo("runas.exe"); 
     procStartInfo.UseShellExecute = true; 
     procStartInfo.CreateNoWindow = true; 

     //Create our arguments 
     string finalArgs = @"/env /user:Administrator """ + subCommandFinal + @""""; 
     procStartInfo.Arguments = finalArgs; 

     //command contains the command to be executed in cmd 
     using (System.Diagnostics.Process proc = new System.Diagnostics.Process()) 
     { 
      proc.StartInfo = procStartInfo; 
      proc.Start(); 
     } 
+0

我确实运行了你在cmd runas/env/user中建议的以下命令:Administrator“cmd/K”dir c:\\\“”即使输入密码,它给出了下面的输出无法运行cmd/k“dir c:\” – purvang

+0

您运行了'runas/env/user:Administrator“cmd/K \”dir c:\\\“”或'cmd runas/env/user:Administrator“cmd/K \”dir c:\\\“”''?首先是你应该运行的。 –

+0

我运行它为runas/env/user:Administrator“cmd/K \”dir c:\\\“”但它给出错误,如下所示无法运行cmd/k“dir c:\” – purvang

相关问题