C#参数中的传递路径

C#参数中的传递路径,c#,visual-studio,path,console,cmd,C#,Visual Studio,Path,Console,Cmd,我在C#中将cmd作为一个进程启动,并希望在参数中传递一个文件路径。怎么做 Process CommandStart = new Process(); CommandStart.StartInfo.UseShellExecute = true; CommandStart.StartInfo.RedirectStandardOutput = false; CommandStart.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; CommandS

我在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();

您需要将文件路径放在双引号中,并像前面提到的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
传递到参数中。

您希望实现什么?执行一个可执行文件,该文件接受参数并返回可存储在文件中的数据。使用逐字字符串文字。我正在尝试运行mysqldump.exe,传递credentails参数,然后指定导出文件。如何在路径中自动添加双斜杠,因为用户通过filebrowser选择其mysqldump.exe,并且url不受应用程序的操纵。@DavidWhite转义(双斜杠\verbtaim字符串文字)仅用于在源中键入路径时。在运行时不需要这样做,只需在文件路径(更新的答案)前后添加双引号,这样,如果路径中有空格,它仍然被视为一个参数。@DavidWhite-我用
OpenFileDialog
示例更新了答案。它在所选文件名的前面加上一个“”,然后将其传递到arguments属性。我用新代码更新了我的问题。问题是,当进程启动时,什么都没有发生,它只是冻结。
string finalPath = String.Format("\"{0}{1}_date_{2}.sql\"", AppDomain.CurrentDomain.BaseDirectory, SQLActions.MySQLDatabase, date);