Set the registry for managing the working directory when associating a file type with an application

I use Inno for the installer, and I want to associate a file type with my application:

Root: HKCR; Subkey: ".rpl"; ValueType: string; ValueName: ""; ValueData: "MyReplay"; Flags: uninsdeletevalue; Root: HKCR; Subkey: "MyReplay"; ValueType: string; ValueName: ""; ValueData: "Replay File"; Flags: uninsdeletekey; Root: HKCR; Subkey: "MyReplay\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\bin\MyApp.ico,0"; Flags: uninsdeletekey; Root: HKCR; Subkey: "MyReplay\shell\open\command"; ValueType: string; ValueName: ""; ValueData: """{app}\bin\MyApp.exe"" ""%1"""; Flags: uninsdeletekey; 

Right now, by double-clicking the .rpl file, the application starts, but the working directory is where the .rpl file is located, so the application crashes because it cannot load data. Is it possible to configure the registry to manage the start / working file for file associations, as well as for the running application? Or should my application be able to get around this?

I want my EXE to run from, for example, C: \ Programs \ MyApp. If you click a shortcut or run it manually, it will be fine. The only exception that I can find is to double-click on the file type associated with the application, Windows sets the working directory EXE to the location of this file.

The problem is that I have development on my PC, and I also install the released version as a regular user. I need to be able to run the dev version without going to the registry and finding paths to the installed production version files.

+4
source share
1 answer

Even if there is a way to have a different working directory for your application listed in the registry, you really have to fix it in the application. The reason is that there are several ways to open the data file with your application, and you simply cannot fix it for all of them. Consider

D: \> c: \ Programs \ MyApp \ MyApp.exe \\ public \ foo \ data.ext

where a program with a file name (as a UNC path) is launched to open as a parameter, executed in a completely different directory. I do such things regularly, and I expect applications to work under these conditions.

Note that setting the working directory to the root path of the application first after the start is not a good idea, since then the relative paths will be wrong. The right decision means changing the application so that it works correctly, regardless of what happens in the current working directory, using internal paths.

Edit:

You are commenting

But then this means that my application is completely registry-dependent. This is a pain for development testing, if every time I run my dev build it goes and downloads all the files for the installed version

but this is not necessarily the case. You can read the path to the executable executable using the GetModuleFileName() function or its equivalent shell in the GUI library (for example, Application.ExeName in Delphi). Installing a working directory from this path is simple, I just don't think it is a good idea, since changing the working directory violates the relative paths that the user can expect from work. It’s not true that

Windows starts the EXE from the location of the file that you double-click. This is apparently an exceptional case; in other cases, the EXE always works from where I expect.

because there can be many ways how an application can be executed with a different working directory than the path to the executable itself. A well-designed program does not assume that they are equal. This makes using the application in scripts or from the command line much more difficult.

+4
source

Source: https://habr.com/ru/post/1307603/


All Articles