C# 如何使用c在命令提示符中执行命令#

C# 如何使用c在命令提示符中执行命令#,c#,C#,我想使用c在CMD中执行这些步骤# 1-CD C:\MySQL\MySQL服务器5.0\bin 2-mysqldump-uroot-ppassword sample>d:\Test\222.sql 手动执行此操作时,我将获得名为“222.sql”的文件 我正在使用下面的代码来做这件事,但遗漏了一些东西。。什么也没发生 public void CreateScript_AAR() { string commandLine = @"CD C:\MySQL\MySQL Serv

我想使用c在CMD中执行这些步骤#

1-CD C:\MySQL\MySQL服务器5.0\bin

2-mysqldump-uroot-ppassword sample>d:\Test\222.sql

手动执行此操作时,我将获得名为“222.sql”的文件

我正在使用下面的代码来做这件事,但遗漏了一些东西。。什么也没发生

public void CreateScript_AAR()
    {
        string commandLine = @"CD C:\MySQL\MySQL Server 5.0\bin\mysqldump -uroot -ppassword sample> d:\Test\222.sql""";
        System.Diagnostics.ProcessStartInfo PSI = new System.Diagnostics.ProcessStartInfo("cmd.exe");
        PSI.RedirectStandardInput = true;
        PSI.RedirectStandardOutput = true;
        PSI.RedirectStandardError = true;
        PSI.UseShellExecute = false;
        System.Diagnostics.Process p = System.Diagnostics.Process.Start(PSI);
        System.IO.StreamWriter SW = p.StandardInput;
        System.IO.StreamReader SR = p.StandardOutput;
        SW.WriteLine(commandLine);
        SW.Close();
    } 

您正在1个命令中执行这两个命令,这是无效的语法。 您甚至不需要更改目录,因此只需从命令字符串中删除dirchange(
CD
)。另外,使用奥德说过的话

string commandLine = @"""C:\MySQL\MySQL Server 5.0\bin\mysqldump"" -uroot -ppassword sample > d:\Test\222.sql";

您也可以直接从代码中调用MySqlDump.exe,而无需cmd.exe。捕获其输出并将其写入文件。以正确的编码写入数据非常重要,这样才能正确恢复数据

    string binary = @"C:\MySQL\MySQL Server 5.0\bin\mysqldump.exe"
    string arguments = @"-uroot -ppassword sample"
    System.Diagnostics.ProcessStartInfo PSI = new    System.Diagnostics.ProcessStartInfo(binary, arguments);
    PSI.RedirectStandardInput = true;
    PSI.RedirectStandardOutput = true;
    PSI.RedirectStandardError = true;
    PSI.UseShellExecute = false;
    System.Diagnostics.Process p = System.Diagnostics.Process.Start(PSI);
    Encoding encoding = p.StandardOutput.CurrentEncoding;
    System.IO.StreamWriter SW = new StreamWriter(@"d:\Test\222.sql", false, encoding);
    p.WaitOnExit();
    string output = p.StandardOutput.ReadToEnd()
    SW.Write(output)
    SW.Close();

这就是我如何使用它来满足我的需求

public static void runResGen()
    {
        string ResGen = @"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\ResGen.exe";
        string outputPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\resxMerge";
        DirectoryInfo dinfo = new DirectoryInfo(outputPath);
        DirectoryInfo latestdir = dinfo.GetDirectories().OrderByDescending(f => f.CreationTime).FirstOrDefault();
        string latestDirectory = latestdir.FullName.ToString();
        string arguments = latestDirectory + "\\StringResources.resx /str:c#,,StringResources /publicClass";
        ProcessStartInfo PSI = new ProcessStartInfo(ResGen, arguments);
        PSI.RedirectStandardInput = true;
        PSI.RedirectStandardOutput = true;
        PSI.RedirectStandardError = true;
        PSI.UseShellExecute = false;
        PSI.CreateNoWindow = true;
        System.Diagnostics.Process p = System.Diagnostics.Process.Start(PSI);
        Encoding encoding = p.StandardOutput.CurrentEncoding;
        p.Close();
    }

这对我帮助很大。谢谢你的提问和回答。