2

After a lot of searching on the internet without any success, I'm looking here for some help.

The problem seems to be quiet simple, but unfortunately I'm not able to solve it.

I want to change the default-application to open .txt-files. For example instead of using notepad I want to use Wordpad which is located at C:\Program Files\Windows NT\Accessories\wordpad.exe

So I've tried to change the registry at: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithProgids with no success.

I've also found a solution which tries to change the group policy. This code looks like:

string tempFile = Path.GetTempFileName();
string xmlFile = tempFile.Replace(".tmp", ".xml");
File.Move(tempFile, xmlFile);

XDocument document = new XDocument(new XElement("DefaultAssociations",
    new XElement("Association",
        new XAttribute("Identifier", ".txt"),
        new XAttribute("ProgId", "txtFile"),
        new XAttribute("ApplicationName", "Editor"))));
document.Save(xmlFile);

ComputerGroupPolicyObject.SetPolicySetting(@"HKLM\Software\Policies\Microsoft\Windows\System!DefaultAssociationsConfiguration",
    xmlFile, RegistryValueKind.String);

But this also doesn't work.

I also tried to use the command-line with ftype but that also didn't work.

Can anybody tell me how to change the assoziated application for a given filetype?

Tomtom
  • 9,087
  • 7
  • 52
  • 95
  • 1
    what do you mean not work? – Daniel A. White Nov 30 '16 at 13:34
  • 1
    This is a rather strange thing to want to do programmatically. The only person who should be changing their default applications is the user, not your program. This smacks of one of those "my application is so awesome, it should be your default everything!" situations. – Cody Gray - on strike Nov 30 '16 at 13:38
  • 1
    @CodyGray -- Well, plenty of commercial software products that I've used give you the option to set certain file extensions to use their application. I don't see the problem with that, as long as the app isn't doing it without telling the user. – rory.ap Nov 30 '16 at 13:40
  • `not work` means that the default-application is not changed – Tomtom Nov 30 '16 at 13:40
  • Oddly my PC doesnt have HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer ... so that would explain why it doesnt work .. its under HKEY_LOCAL_MACHINE.... – BugFinder Nov 30 '16 at 13:51
  • @Tomtom Which Windows OS? According to [this answer](http://stackoverflow.com/a/26031257/1497596) and [this answer](http://stackoverflow.com/a/27004486/1497596) and this [TechNet article](https://blogs.technet.microsoft.com/mrmlcgn/2013/02/26/windows-8-associate-a-file-type-or-protocol-with-a-specific-app-using-gpo-e-gdefault-mail-client-for-mailto-protocol/), Microsoft has made it more difficult since Windows 7 to add or change a file extension association programmatically. – DavidRR Dec 01 '16 at 16:56
  • @Tomtom Arguably, this question is a duplicate of [How to change filetype association in the registry?](http://stackoverflow.com/q/1082889/1497596). However, that question dates back to 2009. So that your question is *not* considered a duplicate, I would like to revise your question so that it specifically concerns either Windows 8 or 10. (Of course, that is if your question is with respect to one of those newer versions of Windows.) – DavidRR Dec 01 '16 at 18:54

1 Answers1

2

I guess you want to this because you have some kind of Set as default option in your program, by the way I have spent the last hour trying to figure out why it doesn't work and here it is what I've found so far.

The step you need to take are the following:

  • Creates a registry key in ClassesRoot for the .custom extension. (Period is important)

Code:

Registry.ClassesRoot.CreateSubKey(".custom").SetValue("", "customfile", Microsoft.Win32.RegistryValueKind.String);`
  • Creating the "Customfile" sub-key and the "customfile\open\command" subkey that is needed to store the path to the application that will open this file type.

Code:

Registry.ClassesRoot.CreateSubKey("Customfile\shell\open\command").SetValue("", PATH_TO_YOUR_EXE, Microsoft.Win32.RegistryValueKind.String);

And now the association has been made, your app will be registered as one of those which can open that extention.


The case of .txt (or other already associated extentions)

After messing a little bit around i found out that in order to do changes to an already associated extention you also need to edit the registry

Example (with .txt ext.)

 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice

This key has an ProgId value which actually contains the default application set by the user, the value is a string. So you will also have do edit/delete this Registry as well.

I hope it helps :)!

Community
  • 1
  • 1
Sid
  • 14,176
  • 7
  • 40
  • 48
  • I'm repeating a comment I left on the question. To which Windows OS does your answer apply? According to [this answer](http://stackoverflow.com/a/26031257/1497596) and [this answer](http://stackoverflow.com/a/27004486/1497596) and this [TechNet article](https://blogs.technet.microsoft.com/mrmlcgn/2013/02/26/windows-8-associate-a-file-type-or-protocol-with-a-specific-app-using-gpo-e-gdefault-mail-client-for-mailto-protocol/), Microsoft has made it more difficult since Windows 7 to add or change a file extension association programmatically. – DavidRR Dec 01 '16 at 16:59
  • @DavidRR Before Win Vista, you can edit the registry without problem, after that you can't do it and you cannot even add it. I've just tested in `Win10` and you can't edit `ProgId` even if you run it as admin, you can delete `UserChoice` folder but cannot add it. The same thing is for `Win8`, unfortunately I cannot state the same for `Win7` as I've not tested yet – Sid Dec 01 '16 at 17:03
  • @DavidRR Actually I think it's okay because in this way apps cannot edit user preferences as they need. And if they do delete the `UserChoice` folder the user is prompted to choose again the app to use – Sid Dec 01 '16 at 17:05
  • Applications such as web browsers and PDF readers offer options in their UI to allow the user to make the application the default for a given document type (for that user only). So, it must be possible to do this programmatically even in Windows 10. – DavidRR Dec 01 '16 at 17:09
  • @DavidRR I will investigate about it and will let you know if I find something :) – Sid Dec 01 '16 at 17:12