简体   繁体   中英

Associate file types with my application C#


I have been trying to get my application to associate certain file types with the app.
The easiest way as I have seen is to change the registry in [HKEY_CLASSES_ROOT].
However, in windows 7 (and i'm guessing up) there is no way to do so getting admin permission. 获得管理员许可就无法这样做。
I have been tangling this issue for a very long time and I would appreciate if someone would point me in the right direction.
Which values do I need to create and change in the registry? To what values? (My software name? What description?)
Thanks alot.

Just an example below of association between .txt files and TxtEditor.exe

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT.txt] @="test.txt"

[HKEY_CLASSES_ROOT\\test.txt] @="Text Document"

[HKEY_CLASSES_ROOT\\test.txt\\DefaultIcon] @="%SystemRoot%\\SysWow64\\imageres.dll,-102"

[HKEY_CLASSES_ROOT\\test.txt\\shell]

[HKEY_CLASSES_ROOT\\test.txt\\shell\\open]

[HKEY_CLASSES_ROOT\\test.txt\\shell\\open\\command] @="\\"C:\\TxtEditor.exe\\" \\"%1\\""

[HKEY_CLASSES_ROOT\\test.txt\\shell\\print]

[HKEY_CLASSES_ROOT\\test.txt\\shell\\print\\command] @="\\"C:\\TxtEditor.exe\\" /p \\"%1\\""

Also have a look at http://www.codeproject.com/Articles/17023/System-File-Association

Found the answer: For that I created a faulty file type and associated it to my program.
Then I searched the registry for changes and copied the pathes of those changes.
The code is here and I would like to here if anyone has a better answer.

`
        public static void SetAssociation(string Extension, string KeyName, string OpenWith, string FileDescription)
        {
            RegistryKey OpenMethod;
            RegistryKey FileExts;

//HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.avi\UserChoice -->"Progid" "Applications\YEPlayer.exe" OpenMethod = Registry.CurrentUser.OpenSubKey(@"Software\Classes\Applications\", true); OpenMethod.CreateSubKey(KeyName + @".exe\shell\open\command").SetValue("",'"'+OpenWith+'"'+ " "+ '"'+"%1"+'"'); FileExts = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\", true); foreach (string child in FileExts.OpenSubKey(Extension).GetSubKeyNames()) { FileExts.OpenSubKey(Extension,true).DeleteSubKey(child); } FileExts.CreateSubKey(Extension + @"\UserChoice").SetValue("Progid", @"Applications\" + KeyName +".exe"); }

'
Thanks alot!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM