1

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.

SagiZiv
  • 932
  • 1
  • 16
  • 38
  • If the path is relative, then it has to be relative to something. Why wouldn't you know that information, _(or at least allow the script to determine it)_? – Compo Jan 15 '20 at 11:46
  • If the end users do not have administrative permissions, I'm amazed they can, or should be, allowed to install your program. Of course if the program is for one user, you could add the information to the Current User registry, `HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts`, instead of the global `LOCAL_MACHINE` keys used by your chosen commands. – Compo Jan 15 '20 at 11:58
  • @Compo It depends on where the user decides to install the program – SagiZiv Jan 15 '20 at 12:47
  • @Compo This is the only part that needs administrative permission, the program itself can run normal without it. How do I modify the registry with some script¿ – SagiZiv Jan 15 '20 at 12:48
  • 1
    Use the search facility, or open up a Command Prompt window and enter `reg /?` to read the help information for the command line tool `reg.exe`. If you're installing the program, you should be able to add the required entries as part of that installation routine, _(you'll need to read the help information for your installer software)_. If the program doesn't require installation, you could still package it and any accompanying files within a self extracting archive. Many self extractor programs have methods of asking for destination input and using that location later as necessary. – Compo Jan 15 '20 at 13:00
  • @Compo I am using an installer program that archiving my program and dependencies (In this case the bat file) and extract itself to wherever the user decides. That is why I don't know where the absolute program path – SagiZiv Jan 15 '20 at 13:17
  • Your question is too broad at this moment in time, and off topic. You would need to tell us more about the installer application and provide the settings you've used within it. Inevitably your question would be, "how do I use this specific program and its installation/configuration options", which would be a case of reading the help information and documentation for that installer program. The other possible question would be, "which program can I use to package/extract/install my program". Both of those questions would be off topic here, as it wouldn't be a code specific programming issue. – Compo Jan 15 '20 at 13:52
  • @Compo Updated the question – SagiZiv Jan 15 '20 at 16:02
  • What third party program are you using to create your executable package? and what configuration options/settings are you using? Also how are those answers related to batch file code? _and not to a lack of understanding/knowledge of your specifically chosen 3rd party program!_ – Compo Jan 15 '20 at 16:19
  • @Compo The 3rd party installer is only used for deployment, not more. The batch code is to associate the file type, as I wrote, I couldn't find different way to do this association – SagiZiv Jan 15 '20 at 16:57
  • It's your question and your choice whether you want assistance, solutions which aren't lucky guesses require sufficient base information. – Compo Jan 15 '20 at 18:01
  • 1
    I recommend to study the commented batch file code posted by me in [UltraEdit forum](http://forums.ultraedit.com/how-to-automatically-open-readme-license-files-wit-t7369.html) to associate all extensionless files or a specific file extension with UltraEdit or UEStudio. You could adapt the code to your needs. – Mofi Jan 15 '20 at 19:07

1 Answers1

1

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.

jwdonahue
  • 6,199
  • 2
  • 21
  • 43