Created
January 23, 2015 10:25
-
-
Save nathanchere/cb678298fdff27796296 to your computer and use it in GitHub Desktop.
File association helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Win32; | |
namespace FerretLib.Windows | |
{ | |
public static class FileAssociationManager | |
{ | |
/// <summary> | |
/// Create or update a file association in Windows | |
/// </summary> | |
/// <param name="fileExtension">The file extension | |
/// <example>txt</example> | |
/// <example>mp3</example> | |
/// </param> | |
/// <param name="executablePath">The full non-relative path to the executable to associate with te file type</param> | |
/// <example>C:\Program Files (x86)\Super Note</example> | |
/// <example>mp3</example> | |
/// <param name="registryEntryId">Generic_TXT</param> | |
/// <param name="fileTypeDescription">Generic text file</param> | |
/// <param name="isForAllUsers"> Determines whether to install for all users or onyl the current user. | |
/// Note that if set to 'true' the applcation must be run with Adminstrator priveleges | |
/// on Windows Vista and later, otherwise an Exception will be thrown. | |
/// </param> | |
public static void SetAssociation(string fileExtension, string executablePath, string registryEntryId, string fileTypeDescription, bool isForAllUsers = false) | |
{ | |
var ClassesRoot = isForAllUsers | |
? Registry.ClassesRoot | |
: Registry.CurrentUser.OpenSubKey(@"Software\Classes"); | |
using (var key = ClassesRoot.CreateSubKey(fileExtension)) | |
{ | |
key.SetValue("", registryEntryId); | |
} | |
using (var key = ClassesRoot.CreateSubKey(registryEntryId)) | |
{ | |
key.SetValue("", fileTypeDescription); | |
key.CreateSubKey("DefaultIcon").SetValue("", "\"" + executablePath + "\",0"); | |
using (var subKey = key.CreateSubKey("Shell")) | |
{ | |
subKey.CreateSubKey("edit").CreateSubKey("command").SetValue("", "\"" + executablePath + "\"" + " \"%1\""); | |
subKey.CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + executablePath + "\"" + " \"%1\""); | |
} | |
}; | |
var regPath = string.Format(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.{0}", fileExtension); | |
using (var key = Registry.CurrentUser.OpenSubKey(regPath, true)) | |
{ | |
key.DeleteSubKey("UserChoice", false); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment