javawindowscmdregistryfile-association

Changing file associations using Java


I have read that you can change the Windows file associations by using commands like assoc .pmdtb="X:\PassswordManager.bat", but I still have some problems with it.

  1. When I run the command in the command line, then nothing changes. Files with this extension are still opened with Notepad.

  2. When I try to run the command in Java, I am denied access.

How do I edit the Windows internal file associations in Java? If I should rather edit the registry directly, which of the many Classes directories?

My Java code:

Runtime.getRuntime().exec("cmd.exe /c assoc .pmdtb=\"X:\\PasswordManager.bat\"");
// --> Access denied.

Thanks in advance.


Solution

  • File associations will apparently no longer be changeable with assoc and ftype starting with Windows 8. A hash code is additionally in the registry, which is generated with a secret algorithm. If I change the file association, but the hash does not match, the default opening of this file extension will not change. The Windows algorithm for the hash has been reverse engineered. Based on this, you could translate the algorithm into Java and change the registry directly.

    The C++ code with QT can be found here: https://pastebin.com/yVhWeQ3X