Command-Line Arguments and File Association via InstallForge

Posted in Code Tutorial by walkingTarget on June 26, 2012 1 Comment

If you’re going to start from scratch like I did, go ahead and create a new project for testing out command-line arguments.  File Association will use your program’s ability to parse command-line arguments, but other than that they’re really separate topics.

Now that you’ve got your project created, you need to add some code so that your program will read the command-line arguments passed to it. The following code snippet is something I wrote as an example for this tutorial. It’s very basic – it just iterates through all the command-line args and writes them to console, opens the file specified by the command-line args, and then dumps the files text contents to the console.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestAssociation
{
    class Program
    {
        static void Main(string[] args)
        {
            //Dump the command-line arguments to the console window
            Console.WriteLine("Command-Line Arguments");
            for (int i = 0; i < args.Length; i++)
                Console.WriteLine("{0}. {1}", i, args[i]);

            //Do a little redundant checking of expected command-line args
            if (args.Length >= 1 && !String.IsNullOrEmpty(args[0]))
            {
                //Output text from file sent by command-line args
                Console.WriteLine("Output text in {0}", args[0]);
                System.IO.StreamReader sr = new System.IO.StreamReader(args[0]);
                Console.WriteLine(sr.ReadToEnd());

                sr.Close();
                sr.Dispose();
            }
        }
    }
}

Be sure to save and build your project before continuing. If you try to run the program now, you won’t see anything special because aren’t any command-line args to parse yet.

Next, lets create an installer for our program. For this tutorial I used InstallForge. I’m not overly impressed with its capabilities, but this was a good chance to learn and experiment with another tool.

When you run InstallForge, you’re greeted by the first in a series of pages that allow configuration of your installer. Here’s a screenshot of the values I used for my little program.

Pretty basic and totally meaningless for the end goal of File Association, so enter whatever you want here.

The next page you want to use is the Files page under Setup, which is on InstallForge’s left toolbar.  Here, I just added my executable file, FileAssociation.exe from the bin\Release folder of my project.

You can also change the default installation path, if you so desire, but again, this choice is meaningless and won’t affect the following steps.

The next page to focus on is the Registry Page under the System group.  On the registry page, click the “Add…” button.  Here you’re prompted to enter the registry change you want to make.  Take a look at the settings I chose, and then I’ll explain them.

Since we’re creating a file association, the root key we want is HKEY_CLASSES_ROOT.  Our Sub key will follow the pattern {ext}\shell\open\command where {ext} is the extension you want to associate with your program (ex: .faf).  I used .faf in this tutorial because, at least on my computer, it was an extension that wasn’t already associated with a program.

Under Value data, fill in <InstallPath>\{ProgramName} “%1″.  This probably requires some explanation.  The <InstallPath> portion is quite literal.  <InstallPath> is a Setup Constant understood by InstallForge to be the path the user has chosen to install your program.  Since we can’t predict where they will install our program, we use the constant to ensure that our registry key matches their selection.  The “%1″ section is also meant to be taken literally.  “%1″ will be the command-line argument to our program.  Windows will help us out by automatically replacing the “%1″ with whichever associated file was opened.  Lastly, the  {ProgramName}.exe will be whatever you named your program.  I named mined TestAssociation.exe – take a look at the whole window below.

Click Ok to add your registry changes.  A screenshot of my changes follows…

Finally, click the build button (which I’ve highlighted in yellow) to build your installer.  Once the program is installed, find an associated file and open it to test your association.

I hope I haven’t been to brief in this tutorial – I may attach the project and source code at a later time.

Good luck and happy coding!