Tag: Windows Registry
Filetype association in C#
by admin on Dec.10, 2009, under Soft-Dev
By night I keep myself occupied with travelling, seeing concerts or even sitting back with a bottle of Jacks philosophically reflecting on life. By day I am a software engineer, working for a R&D company developing software for driving rotary and laser engravers. I specialize in a wide variety of programming languages from the depths of the Windows API in C and C++ to the I don’t want to cause self harm way of C-Sharp (those who are familiar with both these lower and upper levels of programming will understand).
With each day innovatively programming and pushing through new barriers in my own programming experience, I like many others turn to the net in times of need, websites of the likes of msdn.microsoft.com/forums, codeproject.com and for more general terms Google.
Although some online research can easily span a great many hours, and possibly the occasional plea on a MSDN forum I feel compelled to give something back to the community, so Google bots, if you’re out there, please link this, it could save a great many people out there who want to ‘Associate filetypes programmatically for Windows 7, Vista and XP’. I couldn’t believe how difficult it was to find relevant information regarding this on the net, solutions not shrouded by DLLs, just a basic run of the mill C# solution that does what it says on the box. So here it goes:
This came about for the need to automatically associate files of a particular type in an installer I was developing for a software product. Here is the snippet related specifically to associating a filetype of your choosing.
|
private const int HWND_BROADCAST = 0xffff; [DllImport("user32.dll")]
public void CreateExtension(string ExtensionType) string AppType = ExtensionType + "_auto_file"; // ie abc_auto_file Registry.ClassesRoot.CreateSubKey("."+ExtensionType).SetValue("",AppType); Registry.CurrentUser.CreateSubKey(@"Software\Classes\."+ExtensionType).SetValue("",AppType); Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\."+ExtensionType).SetValue("",AppType); Registry.ClassesRoot.CreateSubKey(AppType+@"\shell\open\command").SetValue("","\""+InstallString+"\" \"%1\""); Registry.CurrentUser.CreateSubKey(@"Software\Classes\"+AppType+@"\shell\open\command").SetValue("","\""+InstallString+"\" \"%1\""); string UsersPath = ""; if (arinfo[keyid].Length > 12) // If the Key Name is over 12 Characters long } UsersPath = ""; if (arinfo[keyid].Length > 4) // If the Key Name is over 4 Characters long } // Refresh Registry
}
|