简体   繁体   English

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

[英]How to execute commands in Command Prompt using c#

I want to execute these steps in CMD using c# 我想用c#在CMD中执行这些步骤

1 - CD C:\\MySQL\\MySQL Server 5.0\\bin 1 - CD C:\\ MySQL \\ MySQL Server 5.0 \\ bin

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

On manually doing this, i will get file named "222.sql" 在手动执行此操作时,我将获得名为“222.sql”的文件

I am using the below code to do it, but missing something.. Nothing Happens 我使用下面的代码来做,但遗漏了一些东西..没有什么发生

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

You're executing both commands in 1 command, this is not valid syntax. 您在1个命令中执行这两个命令,这是无效的语法。 You don't even need to change the directory, so just remove the dirchange ( CD ) from your command string. 您甚至不需要更改目录,因此只需从命令字符串中删除目录( CD )即可。 Also, use quotes like Oded said. 另外,像Oded说的那样使用引号。

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

You can also call MySqlDump.exe directly from your code without cmd.exe. 您也可以直接从代码中调用MySqlDump.exe而不使用cmd.exe。 Capture its output and write it to a file. 捕获其输出并将其写入文件。 Writing it in the correct encoding is important so that it restores correctly. 以正确的编码进行编写非常重要,以便正确恢复。

    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();

This is how I used it to meet my requirements. 这就是我用它来满足我的要求的方式。

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

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

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