Associate file type

using System; using System.Runtime.InteropServices; using System.Windows.Forms; using Microsoft.Win32; public static class Associate { [DllImport("Shell32.dll")] static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2); public static void AssociateFileType(string programName, string extension, string fileInfo, bool force = false, int iconIndex = 0) { if (force || Registry.GetValue("HKEY_CLASSES_ROOT\\" + programName, String.Empty, String.Empty) == null) { Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\" + programName, "", fileInfo); Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\" + programName + "\\DefaultIcon", "", "\"" + Application.ExecutablePath + "\"" + "," + iconIndex); Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\" + programName + "\\shell\\open\\command", "", "\"" + Application.ExecutablePath + "\"" + " \"%1\""); Registry.SetValue("HKEY_CURRENT_USER\\Software\\Classes\\." + extension.Trim('.'), "", programName); //this call notifies Windows that it needs to redo the file associations and icons SHChangeNotify(0x08000000, 0x2000, IntPtr.Zero, IntPtr.Zero); } } }
A simple class to associate your own file type with your program!

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.