something strange with new process Arguments

P

pantagruel

Hi,

I'm starting a new process and passing it some arguments to look at a
folder that I have created. The folder gets created correctly. The
arguments are read from an xml file saved with ansi encoding (the xml
file works fine so that is not the problem), but with certain strings
that I expand via a regex replace, so that for example if I have the
following arguments:

<command>"%%FileName%" /O="%%OutputFilesFolder%"</command>

this gets read into a variable unexpandedcommand
and I run

foreach (KeyValuePair<string, string> kvp in SettingsDictionary)
{
Replacementkey = "%%" + kvp.Key + "%";

Unexpandedcommand =
Regex.Replace(Unexpandedcommand, Replacementkey, kvp.Value);

}

where the command %%FileName% gets replaced by the value of FileName
in my Dictionary and so forth.

The expanded result gets passed as an argument to the application, and
I also write it to the event log:

"C:\Copy of test2.doc" /O="C:\Copy of test2_files"

Now the problem is when I pass this argument via StartInfo.Arguments I
get nothing out of the process, it just exits. No error from the
process either. But when I start the command line and call the process
with these arguments that I put in my event log it works fine.

I figure it has to be an encoding problem somewhere among the various
possible encodings and passings of strings going on. But I am stumped
as to which one it could be.

So uh, the phrase I'm looking for is: HELP!!!!
 
J

Jeroen Mostert

pantagruel said:
I'm starting a new process and passing it some arguments to look at a
folder that I have created. The folder gets created correctly. The
arguments are read from an xml file saved with ansi encoding (the xml
file works fine so that is not the problem), but with certain strings
that I expand via a regex replace, so that for example if I have the
following arguments:

<command>"%%FileName%" /O="%%OutputFilesFolder%"</command>

this gets read into a variable unexpandedcommand
and I run

foreach (KeyValuePair<string, string> kvp in SettingsDictionary)
{
Replacementkey = "%%" + kvp.Key + "%";

Unexpandedcommand =
Regex.Replace(Unexpandedcommand, Replacementkey, kvp.Value);

}
First of all, do not use Regex for this, just use string.Replace. Using
Regex will have nasty side effects if "replacementkey" happens to contain
anything that's a valid regex expression, and it's just not necessary here.
Now the problem is when I pass this argument via StartInfo.Arguments I
get nothing out of the process, it just exits. No error from the
process either. But when I start the command line and call the process
with these arguments that I put in my event log it works fine.

I figure it has to be an encoding problem somewhere among the various
possible encodings and passings of strings going on.

You're almost certainly wrong. Strings passed around internally don't
usually suffer from encoding problems. Even if they did, your strings don't
contain any characters that would be expected to cause trouble.

Have you tried the obvious, that is, run a stub application instead that
just prints its arguments to the command line? At this point you apparently
don't even know if the arguments are passed at all or whether there's some
other problem that prevents the process from doing what you want it to do.
 
J

Jeroen Mostert

Jeroen said:
pantagruel wrote:

You're almost certainly wrong. Strings passed around internally don't
usually suffer from encoding problems. Even if they did, your strings
don't contain any characters that would be expected to cause trouble.
Well, some correction/clarification is in order here, because troublesome
characters don't really come into it. Obviously, if the strings were passed
as Unicode while you're expecting non-Unicode (or vice versa) things would
go very wrong. But (assuming you're running on a Windows NT-derivative) the
arguments you set in ProcessStartInfo are passed to the process as Unicode,
and recoded as necessary if the process requests them as ANSI, so there's
little opportunity for encoding problems to arise here.

Also: try setting ProcessStartInfo.UseShellExecute to false.
 
J

Jesse Houwing

Hello Jeroen,
First of all, do not use Regex for this, just use string.Replace.
Using Regex will have nasty side effects if "replacementkey" happens
to contain anything that's a valid regex expression, and it's just not
necessary here.

You're almost certainly wrong. Strings passed around internally don't
usually suffer from encoding problems. Even if they did, your strings
don't contain any characters that would be expected to cause trouble.

Have you tried the obvious, that is, run a stub application instead
that just prints its arguments to the command line? At this point you
apparently don't even know if the arguments are passed at all or
whether there's some other problem that prevents the process from
doing what you want it to do.

Just stepping through it with a debugger might be even easier...
 
P

pantagruel

Well, I ended up solving the problem by single quoting the parts that
needed to be quoted.

ProcessStartInfo.UseShellExecute was set to false, but still giving
the problem. It still seems somewhat strange to me that the use of the
" character worked when ran on the command line, but not when passed
as an argument.

I'm not sure what expectation the process I was passing it to had as
per encoding, since it wasn't mine.
 

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