调用 Windows Shell 并向其传递一些参数 .NET c#

发布于 2024-12-14 14:28:46 字数 190 浏览 1 评论 0原文

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

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

没有成功。有人知道可以帮助我的方法吗?

谢谢。

Here my problem, I want to use gpg.exe to decrypt some data. Before this, I want to test and make an "ipconfig" through the Windows Shell. I've tried :

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

without success. Did someone know a way to help me please?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半边脸i 2024-12-21 14:28:46

看看这个函数(取自

   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;
    }

Take a look at this function (taken from 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;
    }
软糖 2024-12-21 14:28:46
  • 第一个参数是执行的文件,"cmd""C:\Windows\System32\cmd"的快捷方式。
  • 第二个参数是提供给程序的参数。在这里,你不能只写“ipconfig”。您必须使用 /r/c/k 将参数提供给 cmd
    <块引用>

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

 

Process.Start("cmd", "/r ipconfig");
  • The first parameter is the file executed, "cmd" is a shortcut for "C:\Windows\System32\cmd".
  • The second parameter are the arguments to give to the program. Here, you can't just write "ipconfig". You have to use /r or /c or /k to give the arguments to cmd:

    /c or /r : Carries out the command specified by string and then stops.
    /k : Carries out the command specified by string and continues.

 

Process.Start("cmd", "/r ipconfig");
牵你手 2024-12-21 14:28:46

注意,语句中的

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

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

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

Note that in the statement

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

Contrary to the comment in the code, the @ sign only affects the string "/C " and does not the affect the contents of the string arguments. In this case it doesn't hurt anything, but doesn't do anything either.

Contrary to the comment in the code, any \s in arguments would indeed need to be escaped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文