Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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# - Fatal编程技术网

C# 作为命令行参数传递的文件路径中的空格

C# 作为命令行参数传递的文件路径中的空格,c#,C#,我试图将文件路径作为命令行参数传递。如果路径中没有空格,则它可以正常工作,但如果有空格,则无法正常工作。在下面的代码中,commandText“scriptPath”甚至可以使用空格。 但是变量“file1”和“file2”不适用于空格 string scriptFilePath=“@”+string.Format(“\”{0}\”,“D:\\Script\\ScriptFile.txt”); 字符串file1=@“D:\newfolder\file1.png”; 字符串file2=@“D:\

我试图将文件路径作为命令行参数传递。如果路径中没有空格,则它可以正常工作,但如果有空格,则无法正常工作。在下面的代码中,commandText“scriptPath”甚至可以使用空格。 但是变量“file1”和“file2”不适用于空格

string scriptFilePath=“@”+string.Format(“\”{0}\”,“D:\\Script\\ScriptFile.txt”);
字符串file1=@“D:\newfolder\file1.png”;
字符串file2=@“D:\newfolder\file2.png”;
字符串outPutPath=@“D:\New Folder\Output\Report.html”;
string commandText=“/c”+“BCompare.exe”+scriptFilePath+“”+file1+“”+file2+“”+outPutPath;
ProcessStartInfo startInfo=新的ProcessStartInfo();
startInfo.WorkingDirectory=@exePath;
startInfo.FileName=“cmd.exe”;
startInfo.RedirectStandardInput=true;
startInfo.UseShellExecute=false;
startInfo.WindowStyle=ProcessWindowStyle.Hidden;
startInfo.CreateNoWindow=true;
Arguments=commandText;
proc=进程启动(startInfo);

进程WaitForExit()将文件路径用引号括起来。

请先修复代码:

  • 缺少分号
  • 修复代码中的路径并使其可读(使用String.Format和use@)
  • 我认为您希望使用命令行执行“executable.exe”,因此使用“executable.exe”作为startInfo.FileName,只需将其余参数(scriptPath、file1、file2)添加到startInfo.arguments中,并确保所有参数都位于双引号之间
解决方案1

string file1 = @"D:\New Folder\file1.png";
解决方案2

string file2 = "\"D:\\New Folder\\file2.png\"";

格式化你的问题,这是不可读的!你好,Nathan Tuggy,这里的要求是我必须在BCompare.exe中运行一个脚本文件(比较两个图像文件)。因此,我通过命令提示符在BCompare.exe中运行脚本。为了更好地理解,我编辑了上面的代码。在命令提示符下执行的命令是BCompare.exe@D:\Script\ScriptFile.txt D:\New Folder\file1.PNG D:\New Folder\file2.PNG D:\New Folder\Output\Report.html。这里,file1和file2是传递给scriptFile.txt的参数,它将在指定的路径中生成Report.html。我很想将其标记为的副本。您好,谢谢您的回复。文件名已用引号括起来。我试着在文件路径前面加上@符号。这不管用。解决方案1和2:尝试过了,不管用,但我来自不允许(或我无法)格式化代码的android应用程序。我纠正了解决方案2:试试看!