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.

You're need to quote the path if it has spaces. The command line treats a space as a delimiter so it would think something like Documents and Settings, for example, is a command with 3 parameters,'document', 'and', and 'settings'

there are 2 ways to fix, A: single quotes
'D:\New Folder\file.py'
and B: Proper Escaping
"D:\\New\ Folder\\file.py"

you can use double \ for the path or use verbatim @ before the string text..

Edited 1 Year Ago by cyberdaemon: misppled

cyberdaemon is correct, you can use the @ symbol.
@"C:\" is the same as "C:\\". Using @ keeps it a little cleaner.
Just as a tip, I would also suggest using string.Format();. It keeps the code cleaner and more readable in my opinion. So for the command text I would use string commandText = string.Format(@"executable.exe{0} \{1}\ \{2}\ \{3}\, scriptPath, file1, file2, outPutPath) This way the commandText can be readable instead of trying to decipher several concatenations. Hope this helps!

Actually, aside from running the process through cmd instead of running it directly, your code produces the right results. I would suspect the problem is either with cmd or in the way executable is handling the arguments. The first option is easy to check, replace "cmd.exe" with "executable.exe" and remove that from commandText. The second will require examining the code for executable.

Edited 1 Year Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.