简体   繁体   中英

How to execute commands in Command Prompt using c#

I want to execute these steps in CMD using c#

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

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

On manually doing this, i will get file named "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. You don't even need to change the directory, so just remove the dirchange ( CD ) from your command string. Also, use quotes like Oded said.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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