String.Format help (making a path)

R

raffelm

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
cmdline).

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}",
strPatchName,
'"');

and

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

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

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?

Thnx
Matt
 
B

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.
 
B

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
champ.

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.

--Bob
 
J

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)
 
S

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
 
R

raffelm

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

Top