By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

Parsing Command-Line Arguments with InstallContext

P: n/a
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);
}
}
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"=?Utf-8?B?bXJpZWRlbA==?=" <an*******@discussions.microsoft.com>
wrote in news:D9**********************************@microsof t.com:
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?


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/
Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.