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”.
As for viewing in a command window, instead, you will probably be better off inspecting the
Argumentsproperty of yourprocessStartInfoinstance.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:
Since you are using /c, you have quote and special char issues still. Try wrapping your entire
commandStringin a set of quotes.Take this simple example for instance (creating temp.txt manually of course):
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:
Resulting in a command line of:
/c ""C:\WINDOWS\Notepad.exe" "C:\temp.txt""and ultimately opening notepad with your test file.