Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass " to cmd with escape \ p4v does not strip out the \ in c#

Tags:

c#

cmd

perforce

I am trying to create a string to pass to cmd to have P4V create and label a file. The problem is the when I pass the string to cmd the escape \ is seen as part of the file path and causing the file not to be found. Is there a way to pass a " without the \ ?

Here is the code I am using it generates a string in cmd as

p4 tag -l labelname \"c:\folder\filename\"  

P4 needs the quotes outside the string.

Needed:

p4 tag -l labelname "c:\folder\filename"
             string tempString = @" """;
             tempString = tempString + stringToTest;
             stringToTest = tempString + @" """;

            System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
            proc.FileName = @"c:\windows\system32\cmd.exe";
            proc.Arguments = @"/k p4 tag -l " + newLabel + " " + stringToTest;
            System.Diagnostics.Process.Start(proc);}enter code here

Any help appreciated.

Here the actual string that is passed to the process (proc.Arguments). I don't have Perforce on this machine so I can't show what is in cmd.exe.

"/k p4 tag -l labelname \"c:\folder\filename \""

like image 998
coolercargo Avatar asked Nov 19 '25 08:11

coolercargo


1 Answers

I find it a lot clearer when you compose strings using String.Format

proc.Arguments = string.Format("/k p4 tag -l {0} \"{1}\"",
                               "labelName", 
                               @"c:\folder\filename");
like image 96
FloatingKiwi Avatar answered Nov 21 '25 22:11

FloatingKiwi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!