Associate File Extension With Application

Associate File Extension with Application

The answer was a lot simpler than I expected. Windows Explorer has its own override for the open with application, and I was trying to modify it in the last lines of code. If you just delete the Explorer override, then the file association will work.

I also told explorer that I had changed a file association by calling the unmanaged function SHChangeNotify() using P/Invoke

public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription)
{
// The stuff that was above here is basically the same

// Delete the key instead of trying to change it
var CurrentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + Extension, true);
CurrentUser.DeleteSubKey("UserChoice", false);
CurrentUser.Close();

// Tell explorer the file association has been changed
SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
}

[DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);

How can I associate a specific file extension with my application?

Read Hans Passant comment.
Using direclty Setup Deployment Project and defining there file associations is quick and easy. Perfect.

Associate File Extension with Application depending on OS version

I have put a solution here for windows 10: Associate File Extension with Application

It can be easily adapted by creating missing node such as ApplicationAssociationToasts.

Associate file types with my application C#

Found the answer:
For that I created a faulty file type and associated it to my program.

Then I searched the registry for changes and copied the pathes of those changes.


The code is here and I would like to here if anyone has a better answer.


`
public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription)
{
RegistryKey OpenMethod;
RegistryKey FileExts;

//HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.avi\UserChoice -->"Progid" "Applications\YEPlayer.exe"
OpenMethod = Registry.CurrentUser.OpenSubKey(@"Software\Classes\Applications\", true);
OpenMethod.CreateSubKey(KeyName + @".exe\shell\open\command").SetValue("",'"'+OpenWith+'"'+ " "+ '"'+"%1"+'"');

FileExts = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\", true);
foreach (string child in FileExts.OpenSubKey(Extension).GetSubKeyNames())
{
FileExts.OpenSubKey(Extension,true).DeleteSubKey(child);
}
FileExts.CreateSubKey(Extension + @"\UserChoice").SetValue("Progid", @"Applications\" + KeyName +".exe");
}

'


Thanks alot!

How to associate a file extension to a program without making it the default program

Setting the following keys worked for me:

key HKLM/SOFTWARE/Microsoft/Windows/CurrentVersion/App Paths/<progname>: "" = <appPath>

key HKCR/Applications/<progname>/SupportedTypes: <fileExt> = ""
key HKCR/<fileExt>: "" = <progID>

key HKCR/<progID>/OpenWithList/<progName>
key HKCR/<fileExt>/OpenWithList/<progName>
key HKCR/SystemFileAssociations/<fileExt>/OpenWithList/<progName>

delete key and subkey at HKCU/SOFTWARE/Microsoft/Windows/CurrentVersion/Explorer/fileExts/<fileExt>

Create registry entry to associate file extension with application in C++

Your basic overview of the process is found in this MSDN article. The key parts are at the bottom of the list:

  • Register the ProgID

A ProgID (essentially, the file type registry key) is what contains your important file type properties, such as icon, description, and context menu items including applications used when the file is double clicked. Many extensions may have the same file type. That mapping is done in the next step:

  • Register the file name extension for the file type

Here, you set a registry value for your extension, setting that extension's file type to the ProgID you created in the previous step.

The minimum amount of work required to get a file to open with your application is setting/creating two registry keys. In this example .reg file, I create a file type (blergcorp.blergapp.v1) and associate a file extension (.blerg) with it.

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\blergcorp.blergapp.v1\shell\open\command]
@="c:\path\to\app.exe \"%1\""
[HKEY_CURRENT_USER\Software\Classes\.blerg]
@="blergcorp.blergapp.v1"

Now, you probably want to accomplish this programmatically. To be absolutely kosher, you could check for the existence of these keys, and change your program behavior accordingly, especially if you're assuming control of some common file extension. However, the goal can be accomplished by setting those two keys using the SetValue function.

I'm not positive of the exact C++ syntax, but in C# the syntax looks something like this:

Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\blergcorp.blergapp.v1\shell\open\command", null, @"c:\path\to\app.exe \"%1\"");
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\.blerg", null, "blergcorp.blergapp.v1");

Of course you could manually open each sub key, manually create the ProgID and extension subkey, and then set the key value, but a nice thing about the SetValue function is that if the keys or values don't exist, they will automatically be created. Very handy.

Now, a quick word about which hive to use. Many file association examples online, including ones on MSDN, show these keys being set in HKEY_CLASSES_ROOT. I don't recommend doing this. That hive is a merged, virtual view of HKEY_LOCAL_MACHINE\Software\Classes (the system defaults) and HKEY_CURRENT_USER\Software\Classes (the per-user settings), and writes to any subkey in the hive are redirected to the same key in HKEY_LOCAL_MACHINE\Software\Classes. Now, there's no direct problem doing this, but you may run into this issue: If you write to HKCR (redirected to HKLM), and the user has specified the same keys with different values in HKCU, the HKCU values will take precedence. Therefore, your writes will succeed but you won't see any change, because HKEY_CURRENT_USER settings take precedence over HKEY_LOCAL_MACHINE settings.

Therefore, you should take this into consideration when designing your application. Now, on the flip side, you can write to only HKEY_CURRENT_USER, as my examples here show. However, that file association setting will only be loaded for the current user, and if your application has been installed for all users, your application won't launch when that other user opens the file in Windows.

That should be a decent primer for what you want to do. For further reading I suggest

  • Best Practices for File Association
  • File Types and File Association, especially
  • How File Associations Work

And see also my similar answer to a similar question:

  • Associating file extensions with a program

Associate a file extension to an application within C# application

Seems that you haven't enough permissions for writing in the windows registry
Try to add to project app.manifest file with content:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>

or if application have to work without requesting admin rights on each start add ability to self-elevate as shown in this example UAC self-elevation

Associate File Extension with Java Application

You can't associate file extensions to trigger a .jar file on Windows. The only file types you can trigger on Windows are .exe, .pif, .com, .bat, .cmd, so instead of triggering the .jar file, you can trigger a .bat file, which then launches the .jar file.

Create a y.bat file and place it next to your y.jar file and write the following code inside it:

@echo off
title y
start javaw -jar "C:\Users\SomeUsername\Desktop\y.jar" %1

You can change the title and y.jar path as you please, just remember that the path need to be the absolute path. Though the real keyword here is %1, that is the actual path, of the file you clicked.

You can get the value of any parameter using a % followed by it's numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on

%* in a batch script refers to all the arguments (e.g. %1 %2 %3 %4 %5 ...%255)

Now you can simply right-click on any .abt file and press "Open With...", remember to check "Use this app for all .abt files" and then just browse for the y.bat and click "Open". Now each time you double-click a .abt file it will launch your .jar program.


Additionally I've written this post (Associate File Extension with Java Application) after writing this answer.

How to associate a program with a file type, but only for the current user?

If you want to register the association for every user, write your data to

HKEY_LOCAL_MACHINE\Software\Classes

If you want to register the association for the current user only, write your data to

HKEY_CURRENT_USER\Software\Classes

This is how to do the latter:

with TRegistry.Create do
try
RootKey := HKEY_CURRENT_USER;
if OpenKey('\Software\Classes\.myfile', true) then
WriteString('', 'MyAppDataFile');
if OpenKey('\Software\Classes\MyAppDataFile', true) then
WriteString('', 'My Very Own Text File Type');
if OpenKey('\Software\Classes\MyAppDataFile\DefaultIcon', true) then
WriteString('', 'C:\WINDOWS\notepad.exe');
if OpenKey('\Software\Classes\MyAppDataFile\shell\open\command', true) then
WriteString('', 'C:\WINDOWS\notepad.exe "%1"');
finally
Free;
end;
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);

This will associate .myfile files, called "My Very Own Text File Type" so that they will have the icon of notepad.exe and will be opened by notepad.exe. The last line tells Explorer to 'reload' itself to reflect the changes made to the file associations. For instance, Explorer file list views will update. The WinAPI function SHChangeNotify is declared in ShlObj.pas, so you need to uses ShlObj.

Notice that the %1 in shell\open\command will expand to the current file. For instance, if you double-click on C:\some dir\test.myfile, then Explorer will execute the command

C:\WINDOWS\notepad.exe "C:\some dir\test.myfile"


Related Topics



Leave a reply



Submit