如何在c#控制台应用程序中执行CMD命令?

时间:2021-05-02 20:49:56

It's very simple to make a mysqldump in cmd on windows, simply:

在windows上在cmd中创建mysqldump非常简单,只需:

Open cmd and put type mysqldump uroot ppassword database > c:/data.sql

打开cmd并将类型mysqldump uroot ppassword数据库> c:/data.sql。

This results in an SQL dump file for the desired database.

这将生成所需数据库的SQL转储文件。

I'm writing a console application so I may run this command:

我正在编写一个控制台应用程序,因此我可以运行以下命令:

-uroot -ppass databse  > location\data.sql

I tried the following code to no avail:

我试了下面的代码,但没有效果:

System.Diagnostics.ProcessStartInfo procStartInfo =
    new System.Diagnostics.ProcessStartInfo("cmd", "/c " + cmd); 

How might I start a cmd process and send my command successfully?

如何启动cmd进程并成功发送命令?

4 个解决方案

#1


5  

Is there a reason why you don't call mysqldump directly?

为什么不直接调用mysqldump呢?

ProcessStartInfo procStartInfo = 
    new ProcessStartInfo("mysqldump", "uroot ppassword databse > c:/data.sql");

If there is a reason, your code should look like this:

如果有理由,您的代码应该如下所示:

ProcessStartInfo procStartInfo = 
    new ProcessStartInfo("cmd", 
        "/c \"mysqldump uroot ppassword databse > c:/data.sql\"");

Changes:

变化:

  • You where missing "mysqldump" in your cmd variable.
  • 您在cmd变量中丢失了“mysqldump”。
  • You should put the command to be executed in the command line into quotes.
  • 您应该将命令在命令行中执行为引号。

#2


28  

Process cmd = new Process();

cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;

cmd.Start();

/* execute "dir" */

cmd.StandardInput.WriteLine("dir");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
Console.WriteLine(cmd.StandardOutput.ReadToEnd());

#3


1  

Do you run Process.Start(psi) with the ProcessStartInfo instance you have just created?

您是否使用刚刚创建的ProcessStartInfo实例运行Process.Start(psi) ?

Anyway, the following should do the work:

无论如何,下面的工作应该是:

string commandToExecute = @"c:\windows\system32\calc.exe";
Process.Start(@"cmd", @"/c " + commandToExecute);

字符串commandToExecute = @ " c:\ windows \ system32系统\ calc.exe”;的过程。开始(@“cmd”,@“/c”+ commandToExecute);

#4


1  

Executing Batch File in C#

在c#中执行批处理文件

Check it out.

检查出来。

#1


5  

Is there a reason why you don't call mysqldump directly?

为什么不直接调用mysqldump呢?

ProcessStartInfo procStartInfo = 
    new ProcessStartInfo("mysqldump", "uroot ppassword databse > c:/data.sql");

If there is a reason, your code should look like this:

如果有理由,您的代码应该如下所示:

ProcessStartInfo procStartInfo = 
    new ProcessStartInfo("cmd", 
        "/c \"mysqldump uroot ppassword databse > c:/data.sql\"");

Changes:

变化:

  • You where missing "mysqldump" in your cmd variable.
  • 您在cmd变量中丢失了“mysqldump”。
  • You should put the command to be executed in the command line into quotes.
  • 您应该将命令在命令行中执行为引号。

#2


28  

Process cmd = new Process();

cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;

cmd.Start();

/* execute "dir" */

cmd.StandardInput.WriteLine("dir");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
Console.WriteLine(cmd.StandardOutput.ReadToEnd());

#3


1  

Do you run Process.Start(psi) with the ProcessStartInfo instance you have just created?

您是否使用刚刚创建的ProcessStartInfo实例运行Process.Start(psi) ?

Anyway, the following should do the work:

无论如何,下面的工作应该是:

string commandToExecute = @"c:\windows\system32\calc.exe";
Process.Start(@"cmd", @"/c " + commandToExecute);

字符串commandToExecute = @ " c:\ windows \ system32系统\ calc.exe”;的过程。开始(@“cmd”,@“/c”+ commandToExecute);

#4


1  

Executing Batch File in C#

在c#中执行批处理文件

Check it out.

检查出来。