开发者

How to execute commands in Command Prompt using c#

I want to execute these step开发者_JAVA百科s 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();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜