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.

Recommended Answers

All 5 Replies

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..

Member Avatar for lithium112

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.