Problem with environment variable expansion on command line (possible bug)

D

David Levine

There appears to be a bug in the way the .NET runtime expands environment
variables surrounded by quotes when they are command line
arguments used as inputs to an application.



For example, create two environment variables...

set Arg1=C:\SomePath\
set Arg2=C:\Temp\SomeFile.xml

If this is passed to a simple .net application...

PrintArgs.exe /a:%Arg1% /b:%Arg2%

Note that the enviromment variables have no quotes surrounding them.

The application is invoked with two separate arguments...
/a:C:\SomePath\
/b:C:\Temp\SomeFile.xml

In other words, the function Main(string[] args) gets invoked with args set
to two strings.

However, when the same application is invoked but the environment variables
are surrounded by quotes....
PrintArgs.exe /a:"%Arg1%" /b:"%Arg2%"
It receives a single command line argument:
/a:C:\SomePath\" /b:C:\Temp\SomeFile.xml

It strips the 1st quote mark off the 1st string but leaves the trailing
quote mark. Both quote marks are stripped off the 2nd argument.

It also gets the number of arguments wrong. Rather then identifying it as
two separate arguments it considers both strings to be a single argument.

I would consider the correct behavior to be that it should receive the same
arguments in both cases.

When the same arguments are passed to a batch file...
PrintArgs.bat /a:"%Arg1%" /b:"%Arg2%"

....where PrintArgs.bat is a simple batch file that echoes the arguments
passed to it.
It receives two command line arguments:
/a:"C:\SomePath\"
/b:"C:\Temp\SomeFile.xml"

Note that the quotes are still intact. The environment variables are
expanded but the quotes are not removed.

I ran all these on a machine using XP - the results may vary on different
versions of the OS - using .net 2.0.

So, is this is a bug or is this a misunderstanding on my part as to how it
should behave?

Thanks
 
D

David Levine

Was my message unclear; has no one else run into this?

Will someone from MSFT respond?
 

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