Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 命令行参数中的空格无效_C#_Command Line Arguments_Filepath - Fatal编程技术网

C# 命令行参数中的空格无效

C# 命令行参数中的空格无效,c#,command-line-arguments,filepath,C#,Command Line Arguments,Filepath,我需要执行命令行参数。如果文件路径包含空格,则无法正常工作。它返回找不到的错误文件。程序如下所示 public void Method() { string docFile = @"C:\Test Document1.doc"; string docxFile = @"C:\Test Document1.docx"; string file = @"C:\doc2x_r649 (1)\doc2x_r649\doc2x.exe"; ExecuteCommand(f

我需要执行命令行参数。如果文件路径包含空格,则无法正常工作。它返回找不到的错误文件。程序如下所示

public void Method()
{
    string docFile = @"C:\Test Document1.doc";
    string docxFile = @"C:\Test Document1.docx";
    string file = @"C:\doc2x_r649 (1)\doc2x_r649\doc2x.exe";

    ExecuteCommand(file, string.Format(docFile + " -o " + docxFile));
}

public static string ExecuteCommand(string file, string command)
{
    String result;
    try
    {
        //Create a new ProcessStartInfo
        System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo();
        //Settings
        procStartInfo.UseShellExecute = false;
        procStartInfo.CreateNoWindow = false;
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.FileName = file;
        procStartInfo.Arguments = command;
        //Create new Process
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        //Set ProcessStartInfo
        proc.StartInfo = procStartInfo;
        //Start Process
        proc.Start();
        //Wait to exit
        proc.WaitForExit();
        //Get Result
        result = proc.StandardOutput.ReadToEnd();
        //Return
        return result;
    }
    catch
    {

    }

    return null;
}

如果文件路径不包含空格,它将正常工作。

是否尝试在路径中添加引号

ExecuteCommand(file, string.Format("\"" + docFile + "\" -o \"" + docxFile + "\""));

您是否尝试过在路径中添加引号

ExecuteCommand(file, string.Format("\"" + docFile + "\" -o \"" + docxFile + "\""));
试试这个

ExecuteCommand(file, string.Format("\"{0}\" -o \"{1}\"",docFile , docxFile));
试试这个

ExecuteCommand(file, string.Format("\"{0}\" -o \"{1}\"",docFile , docxFile));

如果只需要字符串,为什么要使用
string.Format
concatination@I4V:我使用了问题中发布的原始代码,但你是对的,在这种情况下不需要
string.Format
。如果你只需要字符串,为什么要使用
string.Format
concatination@I4V:我使用了问题中发布的原始代码,但是您是对的,在这种情况下不需要
string.Format