Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old November 16th, 2005, 04:53 AM
mriedel
Guest
 
Posts: n/a
Default Parsing Command-Line Arguments with InstallContext

I'm using the InstallContext class to parse the command-line arguments of a console application. The arguments are in the form of "-file=myFile.txt -flag", and the InstallContext object gives me what I need through the Parameters and IsParameterTrue properties.

However, I can't find any documentation or code samples that confirm that this is an acceptable use of the InstallContext class. I understand the need to use Regex or some other means to parse more complex command-line arguments, but I'm surprised to find no mention of this simple parsing alternative. Is there anything wrong with using InstallContext for this purpose

using System;
using System.Configuration.Install;
using System.Collections.Specialized;

class MyClass
{
public static void Main(string[] args)
{
InstallContext context = new InstallContext(null, args);
StringDictionary parameters = context.Parameters;

string file = parameters["file"];
bool flag = context.IsParameterTrue("flag");

Console.WriteLine("file = {0}", file);
Console.WriteLine("flag = {0}", flag);
}
}
  #2  
Old November 16th, 2005, 04:53 AM
Chris R. Timmons
Guest
 
Posts: n/a
Default Re: Parsing Command-Line Arguments with InstallContext

"=?Utf-8?B?bXJpZWRlbA==?=" <anonymous@discussions.microsoft.com>
wrote in news:D9787DEE-2985-407A-882D-4EBB76BF1D86@microsoft.com:
[color=blue]
> I'm using the InstallContext class to parse the command-line
> arguments of a console application. The arguments are in the
> form of "-file=myFile.txt -flag", and the InstallContext object
> gives me what I need through the Parameters and IsParameterTrue
> properties.
>
> However, I can't find any documentation or code samples that
> confirm that this is an acceptable use of the InstallContext
> class. I understand the need to use Regex or some other means to
> parse more complex command-line arguments, but I'm surprised to
> find no mention of this simple parsing alternative. Is there
> anything wrong with using InstallContext for this purpose?[/color]

I looked at the underlying source code for InstallContext with
Reflector (http://www.aisto.com/roeder/DotNet/). As far as I can
see, InstallContext doesn't produce any side effects, so it looks
safe to use.

Thanks for bringing this class to light. With over 5000 classes in
the framework, it's too easy to overlook a gem like this.

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.