batch-fileassociate

How can I make specific file extension open in my program


I have created a simple program in C#, that saves data into a file with custom extension. The file can be saved anywhere in the user's PC, and the user can load different data file every time.

I have built the program into an exe file, and I deploy it using 3rd party installer. The installer basically packs the exe file and it's dependencies into one installation exe file that the user can run

When the user run the installation file, a basic installation starts: accept license, select installation path…

And the program and it's dependencies are unpacked to the selected installation path.

Now I want to associate the custom file extension that I use for my program to always open in my program.

The simplest solution I found for that was using a bat file that runs after my program is installed (this is handled by the 3rd party installer, I just add the bat file as a dependency).

The bat file basically just run the Assoc and Ftype commands like that:

Assoc .MyExtension=MyProgram
Ftype MyProgram="Absolute Path" "%%1"

But I have 2 problems with that:

1) This commands require administrative permission, which is a problem for my clients to get.

2) The Ftype command needs a full path to the installation path, but all I have is the relative path (which is next to the bat file), because the installation path is determined by the user when he installs it.

;tldr;

My questions are:

1) Can I accomplish that without administration permission?

2) How to use the Ftype with relative path.


Solution

  • Your batch file can determine its own location with '%~dp0'. Assuming you arranged to have your batch file deposited in a location that is a fixed relative distance from your exe, this should solve your problem.

    This subroutine will create a FQPN:

    @rem Create a fully qualified drive/path name with no redundant backslashes.
    @rem Convert all forward slashes to backslashes.
    @rem Convert all '..\' sequences to an absolute path.
    :SetFQPN
      @set %1=%~f2
      @exit /b 0
    

    When called with the batch relative path:

    @call :SetFQPN varName "%~dp0\..\anyrelativepath"
    @echo %varname%
    

    Returns an absolute path in %varName%.

    Use @Compo's reg.exe suggestion to get around needing elevated privilege for the assoc command.