-3

How to start process and run command like this:

mysql -u root --password="some-password" < "some-file.sql" 

Is it possible to do with process.Start()?

I need cross-platform solution (we cannot use cmd.exe).

Maxim
  • 13,029
  • 6
  • 30
  • 45
  • Is this useful? https://stackoverflow.com/questions/650098/how-to-execute-an-sql-script-file-using-c-sharp – Daniel Loudon Sep 02 '18 at 19:08
  • No, because it solved different issue and for SQL Server. I need to solve process start case. In other words how to run shell command with IO stream redirects like > < |. – Maxim Sep 02 '18 at 19:14

1 Answers1

-1

Yes, this is possible through the System.Diagnostics.Process class. You need to set RedirectStandardInput to true, after which you can write the content of a file redirect the standard input of a process, and write the contents of the file to the Process.StandardInput (which is a StreamWriter)

This should get you started:

var process = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = "mysql.exe", // assumes mysql.exe is in PATH
            Arguments = "-u root --password=\"some-password\"",
            RedirectStandardInput = true,
            UseShellExecute = false
        },
    };

process.Start();

process.StandardInput.Write(File.ReadAllText("some-file.sql"));

Update: this is pretty well documented [here]( https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.redirectstandardinput)

jeroenh
  • 26,362
  • 10
  • 73
  • 104
  • It does not answer directly my question. It means that I have to manually parse shell command line. I mentioned the way to pass whole command. I do not see any other way except cmd /c and bash -c depending on platform. – Maxim Sep 02 '18 at 23:44
  • Well, it's the canonical way of doing this in C#... You indeed need to get the executable (first part) of the command line and the argument list, but that doesn't seem such a big hurdle, does it? – jeroenh Sep 03 '18 at 06:19
  • Finally it was much more easy to check OS version and use the following templates (filename, arguments): ("cmd", "/c \"{0}\"") and ("bash", "-c \"{0}\""). I think it can be big 'hurdle' to process something like this: cmd1 < cmd2 | cmd3 > log1. As I wrote it will need command line parser and I did not want to write 2nd cmd or bash. – Maxim Sep 03 '18 at 07:10
  • IO redirection is some sort of well-known term (http://linuxcommand.org/lc3_lts0070.php). So I guessed it is pretty obvious. – Maxim Sep 03 '18 at 14:04
  • Of course it is. Your example suggested just running a tool and piping the contents of a file to stdin. – jeroenh Sep 03 '18 at 19:16
  • Yes, and I wrote 'command like this'. So in theory it can be any. And in real world it can be any - it will be a template specified by admin. And I cannot be sure how much programs will be used and how std- out/in will be used in that template. Everywhere in StackOverflow answers it is suggested to use cmd /c. But I thought it is not very nice solution and asked for cross-platform one. – Maxim Sep 03 '18 at 22:09