string ext = ".grf"; string title = "Graphicprogram"; string extdescription = "The graphicprogram"; FileRegistrationHelper.SetFileAssociation(@"D:\CAD\CAD.exe", ext, title + "." + extdescription); using Microsoft.Win32; using System.IO; public class FileRegistrationHelper { public static void SetFileAssociation(string fullPath, string extension, string progID) { // Create extension subkey SetValue(Registry.ClassesRoot, extension, progID); // Create progid subkey string assemblyFullPath = System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", @""); StringBuilder sbShellEntry = new StringBuilder(); sbShellEntry.AppendFormat("\"{0}\" \"%1\"", fullPath); SetValue(Registry.ClassesRoot, progID + @"shellopencommand", sbShellEntry.ToString()); StringBuilder sbDefaultIconEntry = new StringBuilder(); sbDefaultIconEntry.AppendFormat("\"{0}\",0", fullPath); SetValue(Registry.ClassesRoot, progID + @"DefaultIcon", sbDefaultIconEntry.ToString()); // Create application subkey SetValue(Registry.ClassesRoot, @"Applications" + Path.GetFileName(fullPath), "", "NoOpenWith"); } private static void SetValue(RegistryKey root, string subKey, object keyValue) { SetValue(root, subKey, keyValue, null); } private static void SetValue(RegistryKey root, string subKey, object keyValue, string valueName) { bool hasSubKey = ((subKey != null) && (subKey.Length > 0)); RegistryKey key = root; try { if (hasSubKey) key = root.CreateSubKey(subKey); key.SetValue(valueName, keyValue); } finally { if (hasSubKey && (key != null)) key.Close(); } } }