tags:

views:

196

answers:

4

I've been using System.Diagnostics.Process.Start(). For example:

string target = @"c:\te=mp\test";
System.Diagnostics.Process.Start("explorer.exe", target)

The target variable is actually supplied more dynamically and does on occasion include an "=" sign which is a legal character in filenames and directories.

The issue is that this triggers an error indicating, "The Path 'mp\test' does not exist or is not a directory." It seems that the path argument is cutoff to the left of the "=" character.

Is there a way to escape the "=" characteror otherwise work-around this issue?

+1  A: 

Just use double quotes:

System.Diagnostics.Process.Start("explorer.exe",  @"""c:\te=mp\test""");
ulrichb
+3  A: 

try wrapping it in quotes, e.g.

string target = @"""c:\te=mp\test""";
AdamRalph
Worked a charm.
JR
So this accomplished the goal. Can you tell me why, exactly?
JR
sometimes, the arguments passed to a process via the command line are parsed for structure, e.g. a=b may be interpreted as 'the a parameter has a value of b'. by wrapping the argument in quotes, you are explicitly stating that the value is to be used as a literal string.
AdamRalph
+2  A: 

Put quotes around the offending parameter. For example:

System.Diagnostics.Process.Start("explorer.exe", "\"" + target + "\"");
Greg
Thanks. This syntax is new to me. Why does this work?
JR
@JR - The code appends a quote to the beginning and end of the target parameter. The quotes are escaped using blackslashes. `"\"" == @""""`.
Greg
A: 

I have the same issue, but the same solution will not work for me. I want to open images from filepaths stored in a table. Right now I simply pull the filepaths and say:

Process.Start("Explorer.exe", "V:\Folder\AnotherFolder\Image.jpg") 

However, some of my *.jpg names include the = character. As a result, when the string is passed to Explorer as an arguement only the characters to the right-hand side of the = are read.

For example I have a filepath "V:\Folder\AnotherFolder\3=U1.jpg". However, the argument is shortened to "U1.jpg"

I can surround this string with triple ", as suggested above, but this causes it to open in a web browser, not default image viewer.

I have also tried converting the = into unicode, as suggest elsewhere, but this only causes the same, original issue. Any help would be appreciated.

Andrew
Generally you won't have much luck posting a Question as an Answer. In this case, you're in luck. Remove the "Explorer.exe" arg altogether. Use the triple quotes around the filepath. You'll be in business in no time.
JR