文件路径中的空格作为命令行参数c#传递 [英] Spaces in file path passed as command line argument c#

查看:67
本文介绍了文件路径中的空格作为命令行参数c#传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我试图将文件路径作为命令行参数传递。如果没有空格的路径,它可以正常工作。空格不是。

请在下面找到我的代码。



Hello,

I am trying to pass file path as command line argument. If the path without spaces it works fine. with spaces it is not.
Please find my code below.

string scriptFilePath =  "@" + Directory.GetCurrentDirectory() + "\\" + scriptFile; // exact path
string scriptPath= " \"" + scriptFilePath + "\""
string file1 = "D:\New Folder\file1.png";
string file2 = "D:\New Folder\file2.png";
string outPutPath = "D:\New Folder\Output\Report.html";
string commandText = "executable.exe" + scriptPath + " " + "\"" + file1 + "\"" +" " + "\"" + file2 + "\"" + " " + "\"" + outPutPath + "\""
ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.WorkingDirectory = @exePath;
                    startInfo.FileName = "cmd.exe";
                    startInfo.RedirectStandardInput = true;
                    startInfo.UseShellExecute = false;
                    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    startInfo.CreateNoWindow = true;
                    startInfo.Arguments = commandText;
            try
                    {
                        proc = Process.Start(startInfo);                        
                        return true;
                    }
                    catch (Exception ex)
                    {
                        return false;
                    }





如果我遗漏了什么,请帮帮我。

谢谢。



Please help me out if am missing anything.
Thanks.

推荐答案

您正在使用CMD执行它;请注意,您需要使用 / c 参数告诉CMD您要运行某些内容,例如:

You are using CMD to execute it; note that you need to use the /c argument to tell CMD that you want to run something, like this:
cmd.exe /c executable.exe arguments here



您可以添加 commandText 字符串到 / c 来解决这个问题:


You can prepend your commandText string to /c to solve this:

string commandText = "/c executable.exe" + scriptPath + " " + "\"" + file1 + "\"" +" " + "\"" + file2 + "\"" + " " + "\"" + outPutPath + "\"";


这篇关于文件路径中的空格作为命令行参数c#传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆