1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
/// <summary>
///
/// </summary>
/// <param name="fileExtension">file extension for which the association must be made. always starts with a "." (dot), as in ".html"</param>
/// <param name="extensionClass">code name for the file extension. a registry key will be create with that name, so it must respect the conventions for registry keys. Ex: "FirefoxHTML"</param>
/// <param name="extensionDescription">a human-readable short description of the file type. Ex: "Firefox HTML file"</param>
/// <param name="command">full path the executable to start, folloed by "%1" for passing parameters. Ex: "C:\myDirectory\myProgram.exe" "%1"</param>
/// <returns></returns>
public static bool Associate(string fileExtension, string extensionClass, string extensionDescription, string command, string icon)
{
try
{
using (RegistryKey HKCU_Soft_Classes = Registry.CurrentUser.OpenSubKey(@"Software\Classes", true))
{
using (RegistryKey fileExtensionKey = HKCU_Soft_Classes.CreateSubKey(fileExtension))
{
fileExtensionKey.SetValue(null, extensionClass, RegistryValueKind.String);
}
using (RegistryKey applicationKey = HKCU_Soft_Classes.CreateSubKey(extensionClass))
{
applicationKey.SetValue(null, extensionDescription, RegistryValueKind.String);
using (RegistryKey defaultIconKey = applicationKey.CreateSubKey("DefaultIcon"))
{
defaultIconKey.SetValue(null, icon, RegistryValueKind.String);
}
using (RegistryKey shellKey = applicationKey.CreateSubKey("shell"))
{
using (RegistryKey openKey = shellKey.CreateSubKey("open"))
{
using (RegistryKey commandKey = openKey.CreateSubKey("command"))
{
commandKey.SetValue(null, command, RegistryValueKind.String);
}
}
shellKey.SetValue(null, "open", RegistryValueKind.String);
}
}
}
return true;
}
catch
{
return false;
}
} |
Partager