在c ++中是否有任何函数像'int system("cmd")'<stdlib.h>在cmd提示符中执行命令?

sag*_*gar 0 .net c#

我是c#programing的新手.我想在c#中使用c ++的int system("command string")这样的函数.

例如:我们可以使用system("winrm") in c++"winrm"命令的函数在命令提示符下执行命令.system()functon在stdlib.h头文件中.我想知道c#中是否有这种功能.如果有,请告诉我.或者我可以在c#中使用相同的system()方法吗?如果是,那么告诉我如何使用?谢谢 !!

Mic*_*aga 5

你正在寻找的接缝Process Class.来自MSDN的信息:

提供对本地和远程进程的访问,使您能够启动和停止本地系统进程.

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        public static void Main()
        {
            Process myProcess = new Process();

            try
            {
                myProcess.StartInfo.UseShellExecute = false;
                // You can start any process, HelloWorld is a do-nothing example.
                myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.Start();
                // This code assumes the process you are starting will terminate itself. 
                // Given that is is started without a window so you cannot terminate it 
                // on the desktop, it must terminate itself or you can do it programmatically
                // from this application using the Kill method.
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

评论后添加.

执行命令的最简单方法可能是Process类的静态方法Start(String)或另一个使用参数执行应用程序的Start(String,String).

Process.Start("application.exe");
Run Code Online (Sandbox Code Playgroud)

要么

Process.Start("application.exe", "param1 param2");
Run Code Online (Sandbox Code Playgroud)

以上示例为您提供了更大的灵活性.例如,使用ProcessStartInfo类作为Start方法的参数,您可以重定向控制台应用程序的输入或输出.