0

我在 C# 中将 cmd 作为进程启动,并且我想在参数中传递文件路径。怎么做?

Process CommandStart = new Process();
CommandStart.StartInfo.UseShellExecute = true;
CommandStart.StartInfo.RedirectStandardOutput = false;
CommandStart.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
CommandStart.StartInfo.FileName = "cmd";
CommandStart.StartInfo.Arguments = "(here i want to put a file path to a executable) -arg bla -anotherArg blabla < (and here I want to put another file path)";
CommandStart.Start();
CommandStart.WaitForExit();
CommandStart.Close();

编辑:

        Process MySQLDump = new Process();
        MySQLDump.StartInfo.UseShellExecute = true;
        MySQLDump.StartInfo.RedirectStandardOutput = false;
        MySQLDump.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        MySQLDump.StartInfo.FileName = "cmd";
        MySQLDump.StartInfo.Arguments = "/c \"\"" + MySQLDumpExecutablePath + "\" -u " + SQLActions.MySQLUser + " -p" + SQLActions.MySQLPassword + " -h " + SQLActions.MySQLServer + " --port=" + SQLActions.MySQLPort + " " + SQLActions.MySQLDatabase + " \" > " + SQLActions.MySQLDatabase + "_date_" + date + ".sql";
        MySQLDump.Start();
        MySQLDump.WaitForExit();
        MySQLDump.Close();
4

1 回答 1

1

您需要将文件路径放在双引号中,并使用@SLaks 提到的逐字字符串文字 ( )。

CommandStart.StartInfo.Arguments = @"""C:\MyPath\file.exe"" -arg bla -anotherArg";

OpenFileDialog 示例

using(OpenFileDialog ofd = new OpenFileDialog())
{
    if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
       string filePath = "\"" + ofd.FileName + "\"";

       //..set up process..

       CommandStart.StartInfo.Arguments = filePath + " -arg bla -anotherArg";
    }
 }

更新评论

您可以使用String.Format.

string finalPath = String.Format("\"{0}{1}_date_{2}.sql\"", AppDomain.CurrentDomain.BaseDirectory, SQLActions.MySQLDatabase, date);

然后传入finalPath参数。

于 2013-07-12T14:07:39.867 回答