【问题标题】:Call Windows Shell and Pass it some arguments .NET c#调用 Windows Shell 并传递一些参数 .NET c#
【发布时间】:2011-11-07 15:04:56
【问题描述】:

这是我的问题,我想使用 gpg.exe 来解密一些数据。在此之前,我想通过 Windows Shell 测试并制作一个“ipconfig”。我试过了:

Process.Start("cmd","ipconfig");

没有成功。请问有人知道有什么方法可以帮助我吗?

谢谢。

【问题讨论】:

  • 你想做什么?不能直接启动ipconfig吗?你试过ProcessStartInfo.UseShellExecute标志吗?
  • “没有成功”是什么意思?一个错误?不是预期的结果?

标签: c# .net windows shell


【解决方案1】:

看看这个函数(取自here

   public static string ExecuteCmd(string arguments)
    {
        // Create the Process Info object with the overloaded constructor
        // This takes in two parameters, the program to start and the
        // command line arguments.
        // The arguments parm is prefixed with "@" to eliminate the need
        // to escape special characters (i.e. backslashes) in the
        // arguments string and has "/C" prior to the command to tell
        // the process to execute the command quickly without feedback.
        ProcessStartInfo _info =
            new ProcessStartInfo("cmd", @"/C " + arguments);

        // The following commands are needed to redirect the
        // standard output.  This means that it will be redirected
        // to the Process.StandardOutput StreamReader.
        _info.RedirectStandardOutput = true;

        // Set UseShellExecute to false.  This tells the process to run
        // as a child of the invoking program, instead of on its own.
        // This allows us to intercept and redirect the standard output.
        _info.UseShellExecute = false;

        // Set CreateNoWindow to true, to supress the creation of
        // a new window
        _info.CreateNoWindow = true;

        // Create a process, assign its ProcessStartInfo and start it
        Process _p = new Process();
        _p.StartInfo = _info;
        _p.Start();

        // Capture the results in a string
        string _processResults = _p.StandardOutput.ReadToEnd();

        // Close the process to release system resources
        _p.Close();

        // Return the output stream to the caller
        return _processResults;
    }

【讨论】:

    【解决方案2】:
    • 第一个参数是执行的文件,"cmd""C:\Windows\System32\cmd"的快捷方式。
    • 第二个参数是提供给程序的参数。在这里,你不能只写"ipconfig"。您必须使用/r/c/k 将参数提供给cmd

      /c 或 /r : 执行字符串指定的命令然后停止。
      /k :执行字符串指定的命令并继续。

     

    Process.Start("cmd", "/r ipconfig");
    

    【讨论】:

    • 对不起,我只是怀疑:)
    • 你真的需要硬编码到 cmd.exe 的完整路径吗?它不会在系统路径中找到它吗?
    • @Marco:不用担心 :) 在cmd 帮助中,写着/R is equivalent to /C for compatibility reasons。我不知道应该使用哪一个。
    • 谢谢它的工作,你能告诉我为什么外壳消失得这么快吗?
    • 这是正常行为:/c or /r : Carries out the command specified by string 然后停止。如果您不希望命令窗口在最后关闭,请使用 /k 而不是 /r
    【解决方案3】:

    注意在语句中

    ProcessStartInfo("cmd", @"/C " + arguments);
    

    与代码中的注释相反,@ 符号只影响字符串"/C ",不影响字符串arguments 的内容。在这种情况下,它不会伤害任何东西,但也不会做任何事情。

    与代码中的注释相反,arguments 中的任何 \s 确实需要转义。

    【讨论】:

      猜你喜欢
      • 2012-12-27
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      • 1970-01-01
      相关资源
      最近更新 更多