Passing filename as a command line parameter

  • Thread starter Thread starter jlea
  • Start date Start date
J

jlea

I'm trying to pass a filename, obtained with using the fileName property
from the OpenFileDialog, as a application parameter in
Process.StartInfo.Arguments and run a MFC/C++ application using the Start
method.

When I hardcode the application parameter such as "/name=c:\\myFile.txt" all
is well in the C++ application.

When I use the fileName property to build the parameter, the string becomes
@"/name=c:\myFile.txt" which makes the C++ application unhappy.

How does one get rid of the @ and get the single backslash to a double
backslash to make the C++ application happy?

I'll tried converting the string to a char[] and then rebuilding the string
but as soon as I add the "\\" to the string, the @ is added and only a
single backslash is added.

Thanks.

Jon.
 
jlea said:
I'm trying to pass a filename, obtained with using the fileName property
from the OpenFileDialog, as a application parameter in
Process.StartInfo.Arguments and run a MFC/C++ application using the Start
method.

When I hardcode the application parameter such as "/name=c:\\myFile.txt"
all
is well in the C++ application.

When I use the fileName property to build the parameter, the string
becomes
@"/name=c:\myFile.txt" which makes the C++ application unhappy.

How does one get rid of the @ and get the single backslash to a double
backslash to make the C++ application happy?

I'll tried converting the string to a char[] and then rebuilding the
string
but as soon as I add the "\\" to the string, the @ is added and only a
single backslash is added.

Are you certain that the @ is actually in the string? @ is just the
debugger (and a way in C# code) to indicate that the normal escape
characters do not exist. For example, if you were to hardcode
"c:\\myFile.txt" and @"c:\myFile.txt" you would get the same string.
 
Adam - thanks for responding. You are correct, the @ is actually leading
before the initial ", but I need to have the string contain "\\" and not "\"
since the C++ application needs to have it and I can't seem to get C# to
allow me to get the double backslashes; when I triy to concantenate
"/name=" with the fileName property it always just puts a single backslash.

Jon.


Adam Clauss said:
jlea said:
I'm trying to pass a filename, obtained with using the fileName property
from the OpenFileDialog, as a application parameter in
Process.StartInfo.Arguments and run a MFC/C++ application using the Start
method.

When I hardcode the application parameter such as "/name=c:\\myFile.txt"
all
is well in the C++ application.

When I use the fileName property to build the parameter, the string
becomes
@"/name=c:\myFile.txt" which makes the C++ application unhappy.

How does one get rid of the @ and get the single backslash to a double
backslash to make the C++ application happy?

I'll tried converting the string to a char[] and then rebuilding the
string
but as soon as I add the "\\" to the string, the @ is added and only a
single backslash is added.

Are you certain that the @ is actually in the string? @ is just the
debugger (and a way in C# code) to indicate that the normal escape
characters do not exist. For example, if you were to hardcode
"c:\\myFile.txt" and @"c:\myFile.txt" you would get the same string.
 
jlea said:
Adam - thanks for responding. You are correct, the @ is actually leading
before the initial ", but I need to have the string contain "\\" and not
"\"
since the C++ application needs to have it and I can't seem to get C# to
allow me to get the double backslashes; when I triy to concantenate
"/name=" with the fileName property it always just puts a single
backslash.

Jon.

The double backslash is just a way to visually allow us developers to
specify and view an actual backslash (as opposed to some escape character).
When a string containing a real backslash ("\\" or @"\" whichever way you
want to think about it) gets passed the C++ application, it should realize
that it is actually a backslash and not an escape character. You shouldn't
need do anything there.

Can you show the actual code you are using (on both sides - in the C# app to
create the ProcessStartInfo and then in the C++ app to show how you are
determining the string is incorrect)?
 
I think that I understand your problem.

You are getting a correct file name from the OpenFileDialog, and then
building an argument string for a C++ program. The contents of the
string are correct as your passing it: it contains "c:\myFile.txt". The
problem is that the C++ application is reading the string as if it were
a command-line parameter, and is processing the \m as a special
character, not as a backslash followed by an "m".

If I am correct about this, you will want to use the .Replace()
function on the fileName parameter to fix the string so that the C++
argument parser is happy. Try something like this:

stirng argString = "/name=" + openDialog.FileName.Replace(@"\", @"\\");

This will replace all backslashes with double backslashes. See if this
helps.
 
Thanks for everyone's help. I have not tried Replace (though I may have but
I'll give it a try again) but what I ended up doing was to convert the
string to char[], loop over each character rebuilding the string and when I
found a single back slash in the char[], I added two single slashes. This
seemed to work but not very pretty. I'll try the replace suggestion.

One last question: now that I have successfully built a string of command
line paramaters that makes it to the C++ application ok and I see the
string, the C++ application only thinks there is a single command line
parameter and no more. For example, in the C# application, the command line
is:

"/file=myFile.txt /text=bold" and that string make it into the C++
application but only as a single parameter (I was hoping there would be 2).
When I copied/pasted the string into the C++
application->Properties->Debugging->Command line, I get the same thing until
I remove the quotes and then C++ sees two parameters.

So how does one build a string in C#, requiring quotes, and not have them
show up in __argv[]?

Thanks again.

Jon.
 
Back
Top