C# Process.Start: Whitespace Not Working Even Quoted - c#

What I got:
Process.Start("cmd.exe", "/K \"C:/Program Files/nodejs/node.exe\" \"C:/rc/rainingchain/app.js\"");
Even though I wrapped the filename with escaped ", it still displays the error:
'C:/Program' is not recognized as an internal or external command, operable program or batch file.
What is wrong?

You need to use two " for spaces in program path:
Process.Start("cmd.exe", "/K \"\"C:/Program Files/nodejs/node.exe\" \"C:/rc/rainingchain/app.js\"\"");

You code will be translated to
cmd.exe /K "C:/Program Files/nodejs/node.exe" "C:/rc/rainingchain/app.js"
cmd.exe will translate it to
C:/Program Files/nodejs/node.exe" "C:/rc/rainingchain/app.js
That's why it complain errors.
What you need is to enclose whole node.exe command with double quote again.
Process.Start("cmd.exe", "/K \"\"C:/Program Files/nodejs/node.exe\" \"C:/rc/rainingchain/app.js\"\""); so the node.exe command will be "C:/Program Files/nodejs/node.exe" "C:/rc/rainingchain/app.js"
BTW, why don't just call node.exe directly?
Process.Start("C:/Program Files/nodejs/node.exe", "C:/rc/rainingchain/app.js");

Related

Pass a path with space as an argument

I'm running some code in C# where I call a batch file, and I need to pass a Path with space as an argument and it does not work.
I have tried to call my argument different ways in the batch %1 , %~1, "%1", '%1'. None of these work. Also in my C# code I tried to convert it to string and it wont work either
C# code.
string argument = textBox10.Text.ToString() ;
string command = #"/c powershell -executionpolicy unrestricted X:\PathToBatch\Run.bat" + " " + argument;
System.Diagnostics.Process.Start("cmd.exe", command);
Batch code :
echo %1
Pause
When I pass an argument C:\Program Files\Test as a directory, it prints "C:\Program" and stops at the space.
How can I get the full path ?
Try this :
string command = #"/c powershell -executionpolicy unrestricted X:\PathToBatch\Run.bat"+ " \""+ argument +"\" ";
System.Diagnostics.Process.Start("cmd.exe", command);
This will "write" the following line in your console :
/c powershell -executionpolicy unrestricted X:\PathToBatch\Run.bat "C:\Program Files\Test"
pass your argument double quoted, like so: "C:\Program Files\Test".
the issue is that your argument contains a space so it treated as two different arguments
It worked with simples quotes " '" + arg + "' ", and by calling it in the batch as %~1

Having some trouble executing "svn log" with Process.Start() in LINQPad

Process.Start("svn.exe", "log c:\\work\\lidac\\v1\\ -r {2014-09-01}:{2014-09-24} --xml > c:\\work\\commits.xml");
SVN is throwing an error over the >
Error resolving case of >
I am not sure why. The same command works if I type it directly into the command prompt. Any ideas?
You are passing that redirect output symbol to the svn.exe process. He doesn't understand what > c:\work\commits.xml means. If you want to do the redirect output to a file, you can either write code to get the output from the process object, or try something like:
Process.Start("cmd.exe", #"/C svn log C:\work\lidac\v1\ -r {2014-09-01}:{2014-09-24} --xml > c:\work\commits.xml");

How to view Process.Start() Arguments as they are passed to executable

I'm trying to run a fortran executable with Process.Start and it is not working.
Process proc = new Process();
string args = "<C:\\file.in> C:\\file.out";
proc.StartInfo = new ProcessStartInfo(AppName, args);
proc.Start();
if I paste those arguments into a command window the application runs as expected. proc.Start() does not run as expected.
Any ideas how I can view what Start is actually passing as arguments? My gut feeling is that this is a quotes issue.
The executable launches and hangs, so I'm confident the AppName is getting passed in correctly, it looks like an argument problem.
I tried setting the WorkingDirectory to that of the input and output files as suggested in this question: process.start() arguments but that did not work.
Redirection with the < and > command line operators is a feature that's implemented by the command line processor. Which is cmd.exe. Use its /c argument to execute just a single command:
string args = "/c " + AppName + " < C:\\file.in > C:\\file.out";
proc.StartInfo = new ProcessStartInfo("cmd.exe", args);
proc.Start();
Your args string is exactly what is being passed as arguments to the executable. You can double check it reading your Process ProcessStartInfo.Arguments Property.
Something similar happened to me once, i.e., calling the executable from the command line worked and from code didn't, and it turned out that when called from the command line the executable was running on my PC's [C:] drive, and when called from code it was running on my PC's [E:] drive, which was full!
To check which directory your application is using to run the executable use the Directory.GetCurrentDirectory Method.

Sending arguments to the command line

I need to unzip a compressed file with the command line version of 7zip. This one liner should to the trick:
Process.Start("cmd", #"C:\Users\cw\Downloads\7za920\7za e C:\UPDATED.zip -oc:\");
I'm specifying the path to the 7zip command line executable, and telling it which file to unzip. If I copy and paste those arguments into my command line window, it will work. In C#, it will bring up a command line window, and nothing will happen. What gives?
Try:
Process.Start("cmd", #"/c C:\Users\cw\Downloads\7za920\7za e C:\UPDATED.zip -oc:\");
It's because you're running cmd.exe, and not 7za directly. You can do either of the two:
Process.Start(#"C:\users\...\7za", "e c:\updated.zip -oc:\");
or
Process.Start("cmd", #"/c c:\users\...\7za e c:\updated.zip -oc:\");
The /c flag tells cmd to run the arguments after starting.
Try
Process.Start(#"C:\Users\cw\Downloads\7za920\7za.exe", #"e C:\UPDATED.zip -oc:\");

Displaying command in cmd console

I'm trying to run a command in cmd using C# and am having some difficulties. I'd like to be able to write the command to the cmd console so I can see what it's trying to run (I think there's some issue with the quotes or something, so if I could see the actual string in the command line, I'd be able to see exactly what the problem is). My code looks like this:
var processStartInfo = new ProcessStartInfo("cmd", "/c"+commandString);
processStartInfo.CreateNoWindow = true;
Process.Start(processStartInfo);
So basically, I just want to see the string commandString written in the console. Any help would be greatly greatly appreciated.
string CommandLineString = #"""C:\Program Files\Microsoft SQL Server\100\Tools\Binn\bcp.exe"" ""SELECT * FROM table where date >= '2009-01-01'"" queryout ""C:\Data\data.dat"" -S DBSW0323 -d CMS -n -T";
In this case, the problem is probably just your lack of a space after "/c".
var processStartInfo = new ProcessStartInfo("cmd", "/c " + commandString);
As for viewing in a command window, instead, you will probably be better off inspecting the Arguments property of your processStartInfo instance.
EDIT
Taking into account the command line details you posted, I believe this is what your issue is. Check out the following from cmd help:
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:
If all of the following conditions are met, then quote characters
on the command line are preserved:
no /S switch
exactly two quote characters
no special characters between the two quote characters,
where special is one of: &<>()#^|
there are one or more whitespace characters between the
the two quote characters
the string between the two quote characters is the name
of an executable file.
Since you are using /c, you have quote and special char issues still. Try wrapping your entire commandString in a set of quotes.
Take this simple example for instance (creating temp.txt manually of course):
string commandString = #"""C:\WINDOWS\Notepad.exe"" ""C:\temp.txt""";
var processStartInfo = new ProcessStartInfo("cmd", "/c " + commandString);
The command line to be executed will be: /c "C:\WINDOWS\Notepad.exe" "C:\temp.txt", but this will fail since "C:\temp.txt" is not an executable.
If you wrap the whole thing in one last set of quotes, you should see the intended result:
string commandString = #"""""C:\WINDOWS\Notepad.exe"" ""C:\temp.txt""""";
var processStartInfo = new ProcessStartInfo("cmd", "/c " + commandString);
Resulting in a command line of: /c ""C:\WINDOWS\Notepad.exe" "C:\temp.txt"" and ultimately opening notepad with your test file.
That string is not "written" to the console, it's part of the argument list for a program you launch (which in this case happens to be cmd.exe). Since the console created is owned by that program, unless it wants to print its arguments for its own reasons (which it won't) this is not directly doable.
If you simply want to debug then why not inspect the value of commandString, or write it out into a log file?
If you absolutely need the command line to be displayed in the console then you could resort to hacks (run another intermediate program that prints the command line and then calls cmd.exe with it), but unless there is some other good reason to use this approach I would not recommend it.

Categories

Resources