-1

What I have is a command which is delivered to the cmd to be executed.

string command = @"C:\Privat\Docs\programm.exe" + " " + "-k" + " " + "https://thisIsAnUrl/DoIt=" + argument;

The argument has to look like this: \"xx:xx:xx:xx\"

When I execute it with this command, I'll get the output
C:\Privat\Docs\programm.exe -k https://thisIsAnUrl/DoIt=\"xx:xx:xx:xx\" which is working like I intend it to be.

The problem is: when the directory contains a space (C:\Privat\Do cs\programm.exe) you would need to add quotation marks to the directory:

string command = @"""C:\Privat\Do cs\programm.exe""" + " " + "-k" + " " + "https://thisIsAnUrl/DoIt=" + argument;

The output then become "C:\Privat\Do cs\programm.exe" -k https://thisIsAnUrl/DoIt=\"xx:xx:xx:xx\" which is working fine if I execute it directly in the cmd, but if I call the cmd via c# with this code I get an error that the command was either false written or not found (although it is correct and when i copy exactly this output and, like I said, execute it directly with cmd it is working fine).

What the cmd confuses are the \" before and after xx:xx:xx:xx but it is essential for the command to leave them

What am I missing? Please help

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
Shmosi
  • 322
  • 2
  • 17
  • "I get an error that the command was either false written or not found" - could you post the code and exact error message? – Mathias R. Jessen Jul 20 '16 at 12:49
  • @MathiasR.Jessen The exact Error Message (on german): Der Befehl ___ ist entweder falsch geschrieben oder konnte nicht gefunden werden which translates to: The command ___ was either fasle written or could not be found and the code is irrelevant here it is really just the \" in the end that's the problem – Shmosi Jul 20 '16 at 13:14
  • @DavidPostill thanks for correcting my writing and form mistakes in the question – Shmosi Jul 21 '16 at 06:07

3 Answers3

0

I'd suggest to escape the quotes by adding ^ before of the characters you want/have to escape as described in the link below.

see more: Escape double quotes in parameter

UPDATE

Just try it directly before the backslash \, that you assume to produce the problem, therefore somehow like this: ^\"xx:xx:xx:xx^\". If this won't work, try to also escape the double quotes like this: ^\^"xx:xx:xx:xx^\^". Or the third option: Just escape the double quotes: \^"xx:xx:xx:xx\^"

UPDATE 2

Since this is accepted in the comments, do the following: Make your code look like this:

+ " " + "\"https://thisIsAnUrl/DoIt=\"" + argument;

Community
  • 1
  • 1
danny
  • 358
  • 2
  • 14
  • I see what you mean, but I have actually no clue where to put the ^ – Shmosi Jul 20 '16 at 14:18
  • thanks a lot for me it works like this "^\\^\"xx:xx:xx:xx^\\^\"" , but now my new problem is: the directory I am heading to contains empty spaces let's say: C:\Privat\Do cs\programm.exe I wrote it like this @"""C:\Privat\Do cs\programm.exe""" but the cmd response with (translated from german): >the command "C:\Privat\Do" is either wrong written or could not be found< Any ideas? – Shmosi Jul 21 '16 at 06:05
  • Looks like the cmd doesn't interpret your path as a complete string, so there have to be quotation marks `"` around the string. Try with one more or with an additional single quotation mark before and behind. If my answer solved your originally posted question, please mark it so. – danny Jul 21 '16 at 07:08
  • The original problem is/are the spaces in the directory path (sorry if I didn't point that out clearly) I added another pair of quoation marks before and after the path, but the cmd gives me still the same error (false writen or not found) – Shmosi Jul 21 '16 at 07:40
  • As I told you in the last comment, try with single quotiation marks on both sides. -> Instead of having 3 double quotes in your string, it should look something like that: `@"'C:\Privat\Do cs\programm.exe'"` – danny Jul 21 '16 at 08:02
  • Already tried that. The cmd responses with the error >Der angegebene Pfadname ist ungültig< which translates to >The given (directory) path is invalid< I also tried it with `@'"C:\Privat\Do cs\programm.exe"'` – Shmosi Jul 21 '16 at 08:09
  • My last hope... To make it look like this: `@"C:\Privat\Do cs\programm.exe"` try this in your code: `+ " " + "\"https://thisIsAnUrl/DoIt=\"" + argument;` – danny Jul 21 '16 at 08:20
  • You're welcome. I included the solution in the answer. Don't forget to mark it as solved. – danny Jul 21 '16 at 11:06
0

I think you may just need to set both the Filename and Arguments properties separately when invoking the new process.

Here's some C# code I am using to execute cmd.exe (notice the /c parameter being used here and additional surrounding quotes).

string cmd7z = @"C:\Program Files\7-zip\7z.exe";
string filename = "filetodecompress.bz2";

var processDecompress = new Process()
{
    StartInfo = new ProcessStartInfo()
    {
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        UseShellExecute = false,
        CreateNoWindow = true,
        FileName = "cmd.exe",
        Arguments = $"/c \"\"{cmd7z}\" e -so \"{filename}\"\""
    }
};

processDecompress.Start();
processDecompress.WaitForExit();
int returnCode = processDecompress.ExitCode;
Alex
  • 2,815
  • 2
  • 24
  • 22
  • @Shmosi If you mean my code sample, I just edited my answer and added a missing trailing quote from the Arguments string. I cut and paste and it compiles – Alex Jul 20 '16 at 16:15
  • Otherwise, my guess would be that you don't need to add the back bracket (escaped) quote mark `\"` within your parameter i.e. it should be `C:\Privat\Docs\programm.exe -k https://thisIsAnUrl/DoIt="xx:xx:xx:xx"` – Alex Jul 20 '16 at 16:23
0

This post about escaping command line arguments in c# might solve your problem.

It contains detailed explanation and some examples on how .NET handles escape sequences for command line args.

Community
  • 1
  • 1
ilker
  • 166
  • 2
  • 3