Parsing Web Command Line Args Example

Recently I needed to pass parameters to a smart client exe, so with some assistance from some people on the Develop Mentor lists and some pure hammering of my head against the wall I came up with the following example. I hope this proves useful to others.

The key to getting the web command line arguments is to use the Environment.GetCommandLineArgs() method for an exe started through a web browser this function will return a string array of arguments that looks something like:

string[] args = {
  "C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705\IEExec", 
  "http://localhost/WebCommandLine/WebCommandLine.exe?param1&param2", 
  "3", "1",  "86474707A3C6F63616C686F6374710000000"};

As you can see the second argument is the url of the smart client, which can be parsed to get the parameters after the ‘?’ for the command line arguments for the exe. Remember to give the exe EnvironmentPermission: You must have Read permission to access the "Path" environment variable for the Environment.GetCommandLineArgs() to work.

One problem that is caused by passing parameters to the exe is that the AppDomain that is loaded by the IEExec for the exe sets the config file incorrectly. For the example above it would set the config file to “WebCommandLine.exe?param1&param2.config”. Which if loaded would throw an “ConfigurationException: can’t load xml file WebCommandLine.exe?param1&param2.config”. So the one work around that I found is to set the config file for that AppDomain to the empty string. Like this:

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "");

Instead of passing the empty string you could pass the file name of the correct config file. Although remember that by default IIS will not serve a “.config” file. You can get around this by renaming your config file to something like “app.exe.xml”. You could also get around it by removing the “.config” application mapping from the virtual directory serving the config file, so that it will serve config files.

using System;
using System.IO;
using System.Reflection;
public class MainClass
{
  public const string APP_CONFIG_FILE = "APP_CONFIG_FILE";
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  public static void Main(string[] args)
  {
    // Get Command Line Args
    args = CommandLineArgs(args);
    // Print out the command line parameters
    int parmNumber = 1;        
    foreach(string arg in args)
      Console.WriteLine("Parameter {0}: {1}", parmNumber++, arg);
    Console.ReadLine();
  }
  /// <summary>
  /// This will determine if this exe was executed locally or through a web browser. 
  /// </summary>
  public static bool IsSmartClient
  {
    get
    {
      try
      {         
        new DirectoryInfo(AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
        return false;
      }
      catch 
      {
        return true;
      }
    }
  }
  /// <summary>
  /// This method will parse the command line arguments for a .Net exe's start through
  /// a web browser(Smart Clients).  It will simple split the string after the '?' by the '&'.
  /// </summary>
  /// <remarks>EnvironmentPermission: You must have Read permission to access the "Path" environment 
  /// variable for the Environment.GetCommandLineArgs() 
  /// Command line args passed into main </remarks>
  /// <returns>list of command line args from main or from web command line</returns>
  public static string[] CommandLineArgs(string[] args)
  {
    // If this is not a smart client then return the passed in args
    if(!IsSmartClient)
      return args;
    // If you don't set the config file correctly then you will receive exception like:
    // ConfigurationException: can't file xml file app.exe?param1&param2.config     
    // Correct the location of the config file, I'm setting it to the empty string because
    // I'm not using a config file for this app, but you can set the file to what you want,
    // but remember that by default IIS will not serve a .config file.
    AppDomain.CurrentDomain.SetData(APP_CONFIG_FILE, "");
    // Get the command line args from the environment 
    args = Environment.GetCommandLineArgs();
    // args should look something like: 
    // C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705\IEExec
    //  http://localhost/WebCommandLine/WebCommandLine.exe?param1&param2
    //  3 1 86474707A3C6F63616C686F6374710000000
    
    // Second parameter should be the url
    string url = args[1];
    int startParams = url.IndexOf("?") + 1;
    // If there is no '?' then return  
    if(startParams <= 0)
      return new string[] {};
    // Return the list of parameters split by '&'
    return url.Substring(startParams).Split('&');
  }
}
Published Monday, May 05, 2003 11:14 PM by puzzlehacker

Comments

# re: Parsing Web Command Line Args Example

Actually, the easiest way to override the config file is to include the following in your header:

&lt;link rel="configuration" href="MyApp.exe.config" &gt;

Wednesday, May 07, 2003 6:45 AM by Heath Stewart

# re: Parsing Web Command Line Args Example

I've trying to do this for the last week. I got so far as decoding the parameters but could not stop the config.file from trying to get downloaded.

Friday, November 14, 2003 6:49 PM by Darren Eckersley

# re: Parsing Web Command Line Args Example

Private Shared Sub GetURLStartupArgs()
Dim strArgs() As String = Environment.GetCommandLineArgs()

If strArgs.Length > 1 AndAlso strArgs(1).ToLower.StartsWith("http://") Then

Dim objMatchCollection As MatchCollection
Dim objMatch As Match

objMatchCollection = Regex.Matches(strArgs(1), "(?<Key>[^=#&?]+)=(?<Value>[^=#&]*)")
For Each objMatch In objMatchCollection
_objNameValueCollection.Add(objMatch.Groups("Key").ToString, objMatch.Groups("Value").ToString)
Next

If _objNameValueCollection.Count > 0 Then
Dim strExeName As String = Regex.Match(strArgs(1), "[^/]*.exe", RegexOptions.IgnoreCase).ToString
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", strExeName & ".config")
End If
End If

End Sub

Friday, February 06, 2004 12:06 AM by Jeffy

# Parsing Web Command-Line Args

Parsing Web Command-Line Args

Wednesday, December 29, 2004 9:12 PM by TrackBack

# re: Parsing Web Command Line Args Example

NConsoler is an open source library that provides command line parser functionality based on attribute metadata attached to type.

Library is very easy to add and use in your application. NConsoler gives an ability to display help and validation messages without any line of code.

http://nconsoler.csharpus.com/

Example code:

using System;

using NConsoler;

public class Program {

   public static void Main(params string[] args) {

       Consolery.Run(typeof(Program), args);

   }

   [Action]

   public static void Method(

       [Required] string name,

       [Optional(true)] bool flag) {

           Console.WriteLine("name: {0}, flag: {1}", name, flag);

  }

}

and use it:

program.exe "Maxim" /-flag

Monday, September 08, 2008 1:17 PM by tihobrazov

Leave a Comment

(required) 
(required) 
(optional)
(required)