Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中运行/生成命令与文件_C#_Spawn - Fatal编程技术网

C# 在c中运行/生成命令与文件

C# 在c中运行/生成命令与文件,c#,spawn,C#,Spawn,我可以在C中执行以下操作: var pSpawn = new Process { StartInfo = { WorkingDirectory = @"C:\temp", FileName = fileToRun, CreateNoWindow = true } }; pSpawn.Start(); 这很好用。。。。但是,我想知道是否有一种方法可以运行命令ie:dir/b而不必将其封装在批处理文件中 只需启动cmd.exe并传递所需的参数 va

我可以在C中执行以下操作:

var pSpawn = new Process
     {
         StartInfo = { WorkingDirectory = @"C:\temp", FileName = fileToRun, CreateNoWindow = true }
     };
     pSpawn.Start();

这很好用。。。。但是,我想知道是否有一种方法可以运行命令ie:dir/b而不必将其封装在批处理文件中

只需启动cmd.exe并传递所需的参数

 var pSpawn = new Process
 {
     StartInfo = 
     { 
         WorkingDirectory = @"C:\temp", 
         FileName = "cmd.exe", 
         Arguments ="/K dir /b" }
 };

 pSpawn.Start();
我添加了参数/K,使命令窗口保持打开状态,因此可以看到命令dir的输出。 当然,我认为您对捕获命令的输出非常感兴趣。 在这种情况下,您可以使用以下内容:

StringBuilder sb = new StringBuilder();
var pSpawn = new Process
{
     StartInfo = 
     { 
        WorkingDirectory = @"C:\temp", 
        FileName = "cmd.exe", 
        Arguments ="/c dir /b", 
        CreateNoWindow = true,
        RedirectStandardOutput = true,
        RedirectStandardInput = true,
        UseShellExecute = false
     }
};

pSpawn.OutputDataReceived += (sender, args) => sb.AppendLine(args.Data);
pSpawn.Start();
pSpawn.BeginOutputReadLine();
pSpawn.WaitForExit();
Console.WriteLine(sb.ToString());
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.Arguments = "/c dir /b";
Process.Start(info);

只需启动cmd.exe并传递所需的参数

 var pSpawn = new Process
 {
     StartInfo = 
     { 
         WorkingDirectory = @"C:\temp", 
         FileName = "cmd.exe", 
         Arguments ="/K dir /b" }
 };

 pSpawn.Start();
我添加了参数/K,使命令窗口保持打开状态,因此可以看到命令dir的输出。 当然,我认为您对捕获命令的输出非常感兴趣。 在这种情况下,您可以使用以下内容:

StringBuilder sb = new StringBuilder();
var pSpawn = new Process
{
     StartInfo = 
     { 
        WorkingDirectory = @"C:\temp", 
        FileName = "cmd.exe", 
        Arguments ="/c dir /b", 
        CreateNoWindow = true,
        RedirectStandardOutput = true,
        RedirectStandardInput = true,
        UseShellExecute = false
     }
};

pSpawn.OutputDataReceived += (sender, args) => sb.AppendLine(args.Data);
pSpawn.Start();
pSpawn.BeginOutputReadLine();
pSpawn.WaitForExit();
Console.WriteLine(sb.ToString());
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.Arguments = "/c dir /b";
Process.Start(info);

你可以这样称呼:

StringBuilder sb = new StringBuilder();
var pSpawn = new Process
{
     StartInfo = 
     { 
        WorkingDirectory = @"C:\temp", 
        FileName = "cmd.exe", 
        Arguments ="/c dir /b", 
        CreateNoWindow = true,
        RedirectStandardOutput = true,
        RedirectStandardInput = true,
        UseShellExecute = false
     }
};

pSpawn.OutputDataReceived += (sender, args) => sb.AppendLine(args.Data);
pSpawn.Start();
pSpawn.BeginOutputReadLine();
pSpawn.WaitForExit();
Console.WriteLine(sb.ToString());
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.Arguments = "/c dir /b";
Process.Start(info);

你可以这样称呼:

StringBuilder sb = new StringBuilder();
var pSpawn = new Process
{
     StartInfo = 
     { 
        WorkingDirectory = @"C:\temp", 
        FileName = "cmd.exe", 
        Arguments ="/c dir /b", 
        CreateNoWindow = true,
        RedirectStandardOutput = true,
        RedirectStandardInput = true,
        UseShellExecute = false
     }
};

pSpawn.OutputDataReceived += (sender, args) => sb.AppendLine(args.Data);
pSpawn.Start();
pSpawn.BeginOutputReadLine();
pSpawn.WaitForExit();
Console.WriteLine(sb.ToString());
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.Arguments = "/c dir /b";
Process.Start(info);