How to execute commands on the command line using C #

I want to complete these steps in CMD using C #

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

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

In manual mode, I get a file called "222.sql"

I use the code below for this, but something is missing .. 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(); } 
+4
source share
3 answers

You execute both commands in command 1, this is not valid syntax. You don’t even need to change the directory, so just remove dirchange ( CD ) from the command line. Also use quotes such as Oded.

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

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 right encoding is important for proper recovery.

  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(); 
+2
source

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
source

Source: https://habr.com/ru/post/1309296/


All Articles