2

I created a filetype of my own (.track), and was able to set the association in the publish options of my solution. After installing my program, double clicking on any .track file opens up my application. When this happens, I want the program to load the contents of the file that has been double clicked.

    public static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        if(args.Length == 1)
        {
            MessageBox.Show(args[0]); //For Debugging
            Application.Run(new TrackerForm(args[0]));
        }
        else
        {
            MessageBox.Show(args.Length.ToString()); //Confirms that no arguments are passed.
            Application.Run(new TrackerForm());
        }
    }

I assumed that the path of the double clicked file would be passed to the application via a command line argument. However, double clicking on a file of my associated filetype passes no arguments at all to my executable. What exactly happens when a file of an associated executable is double clicked? What do I need to do to allow my program to load a file that is double clicked in windows explorer?

When I debug using a filepath as a command line argument, my program functions perfectly, so I do not think it is due to an error opening files.

Bill
  • 47
  • 1
  • 8

2 Answers2

1

When you double click a file the shell launches it using the "Open" Verb. The "Open" verb runs the application as follows:

"My Program.exe" "%1"

"%1" is the path of the file you double clicked on. You can check out this MDSN page for more info.

T.J. Moats
  • 533
  • 1
  • 5
  • 8
0

The solution provided by TJ Moats works. However, I wanted to document my steps.

  1. To get to the right place in the registry, you need to know the ProgID of the extension.

    In the solution explorer, you can find it by right clicking on the project name, then Properties > Publish > Options... > File Associations. In my case, these were the settings I gave:

    Extension = .track | Description = File Tracker Data File | ProgID = .trackfile | Icon = Tracker.ico
    
  2. Open up regedit and then navigate to HKEY_CLASSES_ROOT > .[The ProgID] > shell > open > command. In my case, the key is under:

    HKEY_CLASSES_ROOT\.trackfile\shell\open\command
    
  3. Double click on "(Default)" to edit the data of the key. You will need two things, the exact path of your executable, and "%1". In my case, the .exe I made was installed to this directory in App Data:

    C:\Users\Bill\AppData\Local\Apps\2.0\W82CG2C9.L4C\3Q4LH0K1.HXH\file..tion_6762072d39e56a94_0001.0000_f0f46736a74a5052\FileTracker.exe
    
  4. Now, delete the value inside of "(Default)". Replace it with the path of the .exe. Do not include quotes. Follow the path of the .exe with a single space and then "%1". You must include the quotes surrounding the %1.

    In my case, the new value is exactly as it appears in the line below:

    C:\Users\Bill\AppData\Local\Apps\2.0\W82CG2C9.L4C\3Q4LH0K1.HXH\file..tion_6762072d39e56a94_0001.0000_f0f46736a74a5052\FileTracker.exe "%1"
    

Once the new value was accepted, I did not need to do anything else. Now when I double click on a .track file Windows Explorer passes the path of the double clicked file as a command line argument to my FileTracker.exe executable.

However, I need to repeat this process every time I publish a new version, as the new .exe will be in a different folder under AppData.

Bill
  • 47
  • 1
  • 8
  • The fastest way to get the path of your executable is to use the task manager. When the program is running, right click on the process and select "Open File Location". Then hold down the shift key and right click the .exe file. Then select "Copy as Path". You can paste that copied path in Step 4. Just remember to remove the quotes. – Bill Aug 13 '15 at 04:01