Saorrento

Tag: Windows 7

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;
private const int WM_WININICHANGE = 0×001a, WM_SETTINGCHANGE = WM_WININICHANGE, INI_INTL = 1;

[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, uint wMsg, uint wParam, uint lParam);

public void CreateExtension(string ExtensionType)
{

string AppType = ExtensionType + "_auto_file"; // ie abc_auto_file
string FileName = "cipher2.exe";

string InstallPath = @"C:\Program Files\Cipher Technology\symmetry\";
string InstallString = InstallPath + FileName;

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.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\."+ExtensionType+@"\OpenWithList").SetValue("a",FileName);
Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\."+ExtensionType+@"\OpenWithList").SetValue("MRUList","a");
Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\." + ExtensionType + @"\OpenWithProgids").SetValue(AppType, new byte[] { 0 }, RegistryValueKind.Binary);

Registry.ClassesRoot.CreateSubKey(AppType+@"\shell\open\command").SetValue("","\""+InstallString+"\" \"%1\"");
Registry.ClassesRoot.CreateSubKey(AppType+@"\shell\open\command").SetValue("(Default)","\""+InstallString+"\",\"%1\"");
Registry.ClassesRoot.CreateSubKey(AppType+@"\shell\open\command").SetValue("InstallPath",InstallPath);

Registry.CurrentUser.CreateSubKey(@"Software\Classes\"+AppType+@"\shell\open\command").SetValue("","\""+InstallString+"\" \"%1\"");
Registry.CurrentUser.CreateSubKey(@"Software\Classes\"+AppType+@"\shell\open\command").SetValue("(Default)","\""+InstallString+"\",\"%1\"");
Registry.CurrentUser.CreateSubKey(@"Software\Classes\"+AppType+@"\shell\open\command").SetValue("InstallPath",InstallPath);

string UsersPath = "";
string[] arinfo = new string[4];
string PathExt = "";
arinfo = Registry.Users.GetSubKeyNames(); // Get a list of all Sub Key Names in HKEY_USERS
for (int keyid = 0; keyid <= arinfo.Length; keyid++) // For each key
{

if (arinfo[keyid].Length > 12) // If the Key Name is over 12 Characters long
{
              PathExt = arinfo[keyid].Substring(arinfo[keyid].Length - 12, 12); // Get the last 12 characters of the SubKey Name
              if (string.Compare(PathExt, "1000_Classes") == 0) // Does it end in 1000_Classes?
              {
                          UsersPath = arinfo[keyid]; // This subkey is the key we’re looking for
                          break; // exit loop
              }
}
    

}
if (UsersPath.Length > 0) // If we have a valid SubKey name, do the create the following Keys
{
              Registry.Users.CreateSubKey(UsersPath+@"\."+ExtensionType).SetValue("",AppType);
              Registry.Users.CreateSubKey(UsersPath+@"\."+ExtensionType+@"\shell\open\command").SetValue("","\""+InstallString+"\" \"%1\"");
              Registry.Users.CreateSubKey(UsersPath+@"\."+ExtensionType+@"\shell\open\command").SetValue("(Default)","\""+InstallString+"\",\"%1\"");
              Registry.Users.CreateSubKey(UsersPath+@"\."+ExtensionType+@"\shell\open\command").SetValue("InstallPath",InstallPath);
}

UsersPath = "";
arinfo = new string[4];
PathExt = "";
arinfo = Registry.Users.GetSubKeyNames(); // Get a list of all Sub Key Names in HKEY_USERS
for (int keyid = 0; keyid <= arinfo.Length; keyid++) // For each key
{

if (arinfo[keyid].Length > 4) // If the Key Name is over 4 Characters long
{
              PathExt = arinfo[keyid].Substring(arinfo[keyid].Length - 4, 4); // Get the last 4 characters of the SubKey Name
              if (string.Compare(PathExt, "1000") == 0) // Does it end in 1000?
              {
                          UsersPath = arinfo[keyid]; // This subkey is the key we’re looking for
                          break; // exit loop
              }
}
    

}
if (UsersPath.Length > 0) // If we have a valid SubKey name, do the create the following Keys
{
              Registry.Users.CreateSubKey(UsersPath+@"\Software\Classes\."+ExtensionType).SetValue("",AppType);
              Registry.Users.CreateSubKey(UsersPath+@"\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\."+ExtensionType+@"\OpenWithList").SetValue("a",FileName);
              Registry.Users.CreateSubKey(UsersPath+@"\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\."+ExtensionType+@"\OpenWithList").SetValue("MRUList","a");
              Registry.Users.CreateSubKey(UsersPath + @"\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\." + ExtensionType + @"\OpenWithProgids").SetValue(AppType, new byte[] { 0 },                             RegistryValueKind.Binary);
              Registry.Users.CreateSubKey(UsersPath+@"\Software\Classes\."+ExtensionType+@"\shell\open\command").SetValue("","\""+InstallString+"\" \"%1\"");
              Registry.Users.CreateSubKey(UsersPath+@"\Software\Classes\."+ExtensionType+@"\shell\open\command").SetValue("(Default)","\""+InstallString+"\",\"%1\"");
              Registry.Users.CreateSubKey(UsersPath+@"\Software\Classes\."+ExtensionType+@"\shell\open\command").SetValue("InstallPath",InstallPath);
}

// Refresh Registry
SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, INI_INTL); // Force Windows to Re-Load the Registry

}

1 Comment :, , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!


Saorrento Stats