String.Format help (making a path)



I'm struggling to find a way to include long path names in a command
line argument string that I have to build at runtime.

I need to create a string like -o:"c:\my documents\my file.txt".

Everything I have tried so far causes the program I am calling to fail
(I know it can accept long file names as I have tried it from the

The problem I am having is using the Format command and getting "
(quotes) around the my file name.

I have tried

string strArgs = String.Format(@"-o:{3}{0}{3}",


string strArgs = String.Format("-o:\"{0}\"",

string strArgs = String.Format(@"-o:""{0}""",

and none of it gives me a string like -o:"c:\my documents\my
file.txt". I keep getting strings like -o:\"c:\\my documents\\my
file.txt\" which doesnt work.

I've thought about converting the long file name to a short pathname,
which would work if the file exists (and in most cases it will not).

So, I am wondering if there is a better way to get this right?


Ben Bloom

could you do something like (untested)

string strArgs = "-o:\"" + strPatchName + "\"";

Take a look at the results outside of the debugger (print them to the
console yourself) as the debugger adds escape characters to strings.

Bob Grommes

Hm. I'm sure I've done stuff like this without any trouble before. Are you
sure that strPatchname doesn't already contain the double backslashes before
you get to this String.Format() call? The second example should work like a

If you can't get it to do so, though, I'd take the other poster's advice and
do a string concatenation or else a series of Append/AppendFormat calls to
an intermediate StringBuilder.


James Curran

raffelm said:
I keep getting strings like -o:\"c:\\my documents\\my
file.txt\" which doesnt work.

Are you sure that's what you are getting, or is that just what being
displayed in the debug window? (That *would* be what is displayed in the
debug window if the string was what you wanted)

Stoitcho Goutsev \(100\) [C# MVP]

Hi raffelm,

All of them works just fine. The only thing I can think of is that you
already have those double backslashes in the strPatchName


After I printed it out to the output tab, I was able to make a change
in the format string and the command worked.

The debugger really is deceptive for these types of debugging.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question