473,503 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Console application, command line parameter issue

Hello:

I am building a console application and I am having an issue with the
command line arguments. A couple of my arguments are paths and they are
usually enclosed in double quotes. If one of the paths ends up in "\", it
causes the closing double quote to be ignored and the argument gets
concatenated with all the subsequent arguments until another double quote is
found or until the end of the string.

Example:

This command line parameter string:

arg1 "c:\arg2\" arg3

results in only two arguments stored in the args string[] parameter of
the Main method: 'arg1' and 'c:\arg\" arg3' instead of three arguments as
expected.

Is this an expected behavior? If so, is there any way around it?

Thanks in advance,
Ramon
Dec 5 '07 #1
2 3956
Hi Ramon,

Yes, this behavior is by design.

During the startup of the CLR, the CLR loader will call GetCommandLine
Win32 API to obtain the command-line in a single string. Then it will parse
the string into argument components. The parsing algorithm is similar to
the Environment.GetCommandLineArgs() method. Please refer to the remark
section of the link below:
http://msdn2.microsoft.com/en-us/lib...tcommandlinear
gs.aspx

"If 2n backslashes are followed by a quotation mark, the command-line
argument contains n backslashes, and quoted text begins if previous text
was unquoted or ends if previous text was quoted. If 2n+1 backslashes are
followed by a quotation mark, the command-line argument contains n
backslashes and a literal quotation mark. If n backslashes are not followed
by a quotation mark, the command-line argument contains n backslashes."

Since we are using one backslash with quotation mark and 0*2+1 = 1, the
algorithm will set the variable "n" to be zero. So zero backslashe will be
provide for the final arguments, which caused the second and third
arguments to be merged into single one.

To resolve this problem, we should use two backslashes in the second
arguments "c:\arg2\\" arg3.

Note: this is not not a code defeat but by design, since backslashe are
normally used to escape certain special character. Also, this algorithm is
the same as Win32 command line arguments parsing function
CommandLineToArgvW. See the Remarks section below:
http://msdn2.microsoft.com/en-us/library/bb776391.aspx

Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 6 '07 #2
Thank you for your quick and detailed response Jeffrey.

- Ramon
""Jeffrey Tan[MSFT]"" wrote:
Hi Ramon,

Yes, this behavior is by design.

During the startup of the CLR, the CLR loader will call GetCommandLine
Win32 API to obtain the command-line in a single string. Then it will parse
the string into argument components. The parsing algorithm is similar to
the Environment.GetCommandLineArgs() method. Please refer to the remark
section of the link below:
http://msdn2.microsoft.com/en-us/lib...tcommandlinear
gs.aspx

"If 2n backslashes are followed by a quotation mark, the command-line
argument contains n backslashes, and quoted text begins if previous text
was unquoted or ends if previous text was quoted. If 2n+1 backslashes are
followed by a quotation mark, the command-line argument contains n
backslashes and a literal quotation mark. If n backslashes are not followed
by a quotation mark, the command-line argument contains n backslashes."

Since we are using one backslash with quotation mark and 0*2+1 = 1, the
algorithm will set the variable "n" to be zero. So zero backslashe will be
provide for the final arguments, which caused the second and third
arguments to be merged into single one.

To resolve this problem, we should use two backslashes in the second
arguments "c:\arg2\\" arg3.

Note: this is not not a code defeat but by design, since backslashe are
normally used to escape certain special character. Also, this algorithm is
the same as Win32 command line arguments parsing function
CommandLineToArgvW. See the Remarks section below:
http://msdn2.microsoft.com/en-us/library/bb776391.aspx

Hope it helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1624
by: Fred Iannon | last post by:
All, I have developed an application that needs to run in one of two modes: (1) IF NO command line parms are provided I would like it to run as a Window form application (i.e. /target:winexe)...
1
5350
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
1
18263
by: Michael Hetrick | last post by:
How would I pass parameters to a console application? I would like to do something like this: consoleapp.exe /o \\fileshare\origindirectory /d \\fileshare\destinationdirectory I'm not sure...
2
9954
by: Spike_TJ | last post by:
I need some help debugging a console application that requires command line parameters. Normally I start a console application with Ctrl+F5. But this one requires a parameter when started How do...
6
3222
by: Mark Allison | last post by:
Hi, I have an application that I want to be to run in Console mode and GUI mode. If no params are entered, I want the GUI fired up, if params are entered, then go into console mode. I believe...
1
489
by: Jim Heavey | last post by:
I am starting to try to write a console application and I have question about how to launch these type of applications. I create a "driver" class which will execute the appopriate functionality...
2
4732
by: Steve | last post by:
I have created a console app that simply prints out a message a couple times, then exits, here is the code: <code> for(int i = 0; i < 10; i++) { System.Threading.Thread.Sleep(500);...
10
6288
by: Stephany Young | last post by:
When one uses the System.Diagnostics.Process.Start method to launch a common or garden Console application, one can set the WindowStyle property of the StartInfo object to ProcessWindowStyle.Hidden...
3
10097
by: Alex | last post by:
Hello, I'm wroting a console utility in VB 2005, and I need the ability to pass parameters to the application when the program runs. For example, if my program is called testing.exe, I need to...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.