简体   繁体   English

C#传递参数中的路径

[英]C# pass path in argument

I'm starting cmd as a process in C# and I want to pass a file path in the argument. 我正在将cmd作为C#中的进程启动,我想在参数中传递文件路径。 How to do it? 怎么做?

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

EDIT: 编辑:

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

You need to put the file path in double quotes and use a verbatim string literal ( @ ) as SLaks mentioned. 您需要将文件路径放在双引号中,并使用逐字字符串文字( @ )作为SLaks提及。

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

Example with an OpenFileDialog 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";
    }
 }

Update to comment 更新以发表评论

You can format your string using String.Format . 您可以使用String.Format格式化字符串。

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

Then pass finalPath into the arguments. 然后将finalPath传递到参数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM