Search code examples
c#mysqlshellcmd

How to run a mysqlcheck through c#


I'm currently trying to do a basic mysqlcheck through the command prompt. The mysql.exe file itself is within a bin that the working directory is pointing towards to use as part of the commands though the cmd.

Right now I'm getting stuck to where it is stopping while opening the shell. It doesn't proceed to initiate the standardinput.

   {
        Process proc = new Process();
        proc.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
        proc.StartInfo.WorkingDirectory = @"C:\Program Files (x86)\MySQL\bin";

        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardInput = true;
        proc.StartInfo.Arguments = @"Mysqlcheck -u root -c -ppassword database";

        proc.Start();
        string output = proc.StandardOutput.ReadToEnd();            
       proc.StandardInput.WriteLine("Mysqlcheck -u root -c -ppassword database");
        proc.WaitForExit();


    }

Solution

  • Add the /C option before the name of the executable

      proc.StartInfo.Arguments = @"/C Mysqlcheck.exe -u root -c -ppassword database";
    

    Without this argument the cmd shell doesn't execute the command but exits immediately.
    You can see other arguments for the CMD.exe opening a command prompt and typing CMD /?