Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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.
Posted
Comments
Harsh Athalye 22-Dec-14 4:02am    
Can you please show how the commandText value looks like?

1 solution

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

You can prepend your commandText string to /c to solve this:
C#
string commandText = "/c executable.exe" + scriptPath + " " + "\"" + file1 + "\"" +" " + "\"" + file2 + "\"" + " " + "\"" + outPutPath + "\"";
 
Share this answer
 
Comments
Kothai Krishnamoorthy 23-Dec-14 0:47am    
Hello,
Thank you for your reply. I tried it that doesnt solve my problem.
Thomas Daniels 23-Dec-14 2:50am    
In that case, it might be a problem with the executable.exe file, but as I don't know what's in there, I cannot look for the problem.
Kothai Krishnamoorthy 23-Dec-14 3:32am    
Hi, Here the requirement is I have to run a script file( to compare two image files) in BCompare.exe. So, am running the script in BCompare.exe through command prompt.
For better understanding I will repost my code.

My command to be executed in command prompt is

BCompare.exe @D:\Script\ScriptFile.txt D:\New Folder\file1.PNG D:\New Folder\file2.PNG D:\New Folder\Output\Report.html

file1 & file2 are arguements passed to the scriptFile.txt, which will generate Report.html in the path specified.

My c# code is
string scriptFilePath = "@" + string.Format("\"{0}\"", "D:\\Script\\ScriptFile.txt");
string file1 = @"D:\New Folder\file1.png";
string file2 = @"D:\New Folder\file2.png";
string outPutPath = @"D:\New Folder\Output\Report.html";

string commandText = "/c " + "BCompare.exe" + scriptFilePath + " " + 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;
proc = Process.Start(startInfo);
proc.WaitForExit();
Thomas Daniels 23-Dec-14 3:35am    
I never used BCompare, so I'm afraid I cannot help you with that, I'm sorry.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900