views:

4893

answers:

6

I'm trying to execute multiple commands without create a new process each time. Basically, I want to start the DOS command shell, switch to the MySQL command shell, and execute a command. Here's how I am calling the procedure (also below). Also, how do I handle the "\"'s in the command?

ExecuteCommand("mysql --user=root --password=sa casemanager", 100, false);

ExecuteCommand(@"\. " + Environment.CurrentDirectory + @"\MySQL\CaseManager.sql", 100, true);

private void ExecuteCommand(string Command, int Timeout, Boolean closeProcess)
{
    ProcessStartInfo ProcessInfo;
    Process Process;

    ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command);
    ProcessInfo.CreateNoWindow = false;
    ProcessInfo.UseShellExecute = false;
    Process = Process.Start(ProcessInfo);
    Process.WaitForExit(Timeout);

    if (closeProcess == true) { Process.Close(); }
}
+2  A: 

Couldn't you just write all the commands into a .cmd file in the temp folder and then execute that file?

M4N
+6  A: 

You can redirect standard input and use a StreamWriter to write to it:

        Process p = new Process();
        ProcessStartInfo info = new ProcessStartInfo();
        info.FileName = "cmd.exe";
        info.RedirectStandardInput = true;
        info.UseShellExecute = false;

        p.StartInfo = info;
        p.Start();

        using (StreamWriter sw = new StreamWriter(p.StandardInput))
        {
            if (sw.BaseStream.CanWrite)
            {
                sw.WriteLine("mysql -u root -p");
                sw.WriteLine("mypassword");
                sw.WriteLine("use mydb;");
            }
        }
scottm
I could not get this to compile. I had to remove the new StreamWriter
Nick
A: 

A command-line process such cmd.exe or mysql.exe will usually read (and execute) whatever you (the user) type in (at the keyboard).

To mimic that, I think you want to use the RedirectStandardInput property: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardinput.aspx

ChrisW
+1  A: 

You could also tell MySQL to execute the commands in the given file, like so:

mysql --user=root --password=sa casemanager < CaseManager.sql
Ben Hoffstein
A: 

Thanks guys. So either this call OR the code below should work right? It's still failing and I think it is because of the "\"'s in the file path.

ExecuteCommand("mysql --user=root --password=sa casemanager < " + Environment.CurrentDirectory + @"\MySQL\CaseManager.sql", 100, false);

OR....

 Process p = new Process();
 ProcessStartInfo info = new ProcessStartInfo();
 info.CreateNoWindow = false;
 info.FileName = "cmd.exe";
 info.RedirectStandardInput = true;
 info.UseShellExecute = false;

 p.StartInfo = info;
 p.Start();

 StreamWriter sw = p.StandardInput;

 if (sw.BaseStream.CanWrite)
    {
      sw.WriteLine("mysql -u root -p");
      sw.WriteLine("sa");
      sw.WriteLine(@"\. " + Environment.CurrentDirectory + @"\MySQL\CaseManager.sql");
     }

 sw.Close();
 p.WaitForExit();
 p.Close();
Can you change the current directory to the MySQL folder (the one containing your CaseManager.sql) script? In that case you wouldn't need to provide the absolute path, just the filename.
Ben Hoffstein
A: 

i cant get this to work.. Process p = new Process(); ProcessStartInfo info = new ProcessStartInfo(); info.FileName = "cmd.exe"; info.RedirectStandardInput = true; info.UseShellExecute = false;

    p.StartInfo = info;
    p.Start();

    using (StreamWriter sw = new StreamWriter(p.StandardInput))
    {
        if (sw.BaseStream.CanWrite)
        {
            sw.WriteLine("/k cd c:\\program files\\itms");
            sw.WriteLine("itmstransporter -k some other sitches etc..");
            sw.WriteLine("and some more switched here too and other cmd params");
        }
    }

However my main problem is that it fails at stream writer - could you explain what streamwriter is.

Thank you very much..

Olly

olly