简体   繁体   English

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

[英]Call Windows Shell and Pass it some arguments .NET c#

Here my problem, I want to use gpg.exe to decrypt some data. 这是我的问题,我想使用gpg.exe解密一些数据。 Before this, I want to test and make an "ipconfig" through the Windows Shell. 在此之前,我想通过Windows Shell测试并制作一个“ ipconfig”。 I've tried : 我试过了 :

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

without success. 没有成功。 Did someone know a way to help me please? 有人知道一种可以帮助我的方法吗?

Thanks. 谢谢。

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

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 . 与代码中的注释相反, @符号仅影响字符串"/C " ,而不影响字符串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. 与代码中的注释相反,确实需要对arguments \\进行转义。

  • The first parameter is the file executed, "cmd" is a shortcut for "C:\\Windows\\System32\\cmd" . 第一个参数是执行的文件, "cmd""C:\\Windows\\System32\\cmd"的快捷方式。
  • The second parameter are the arguments to give to the program. 第二个参数是提供给程序的参数。 Here, you can't just write "ipconfig" . 在这里,您不能只写"ipconfig" You have to use /r or /c or /k to give the arguments to cmd : 您必须使用/r/c/k将参数提供给cmd

    /c or /r : Carries out the command specified by string and then stops. / c或/ r:执行字符串指定的命令,然后停止。
    /k : Carries out the command specified by string and continues. / k:执行字符串指定的命令并继续。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM