C#代码,用于在Windows 8中使用带有哈希的自定义progid更改附加扩展名的默认程序

目的:将新progid与扩展名相关联,以便使用新的关联程序打开该文件。

编程语言:C#

描述:

我想创建一个程序,将另一个程序与我推荐的程序列表中的扩展名相关联。 我的程序在windows-xp和windows-7中工作,但它在windows-8中不起作用。 当我搜索这个问题时,我发现在Windows-8中还有一个名为“Hash”的附加键。

我无法找到新progid的哈希值。

被跟踪的步骤:

HKEY_CLASSES_ROOT创建了一个类“MyTest.txt”,例如: HKEY_CLASSES_ROOT MyTest.txt Shell Open Command (Default) "[PATH TO NOTEPAD] "%1""

我注意到在LOCAL_MACHINE文件夹中也创建了相同的密钥

现在我想将此“MyTest.txt”ProgID指定给

[HKEY_CURRENT_USER \软件\微软\的Windows \ CurrentVersion \ Explorer中\ FileExts.txt \ UserChoice]

“散列”= “????”

“PROGID”= “MyTest.txt”

但我无法在C#中为我新创建的ProgId“MyTest.txt”找到Hash。

使用C#的代码:

 public void changeExtensionDefaultProgram(string fileext,string operationmode, string oldkeyname, string fileopenerpath) { try { if (!string.IsNullOrEmpty(fileext)) { //Global declaration for new custom key string sCustomkeyName = string.Format("MYTest.{0}", fileext); RegistryKey OurKey = Registry.LocalMachine; RegistryKey ParentKey = Registry.LocalMachine; RegistryKey GlobalLocalMachineKey = Registry.LocalMachine; RegistryKey GlobalRootKey = Registry.ClassesRoot; string keyToCopy = @"SOFTWARE\Classes"; ParentKey = ParentKey.OpenSubKey(keyToCopy, true); string programopencommand = string.Format(@"SOFTWARE\Classes\{0}\Shell\{1}\Command", oldkeyname, operationmode); OurKey = OurKey.OpenSubKey(programopencommand, true); if (OurKey != null) { //check if backup exists then do not take backup, along with source key string backupkeyName = string.Format("MyBKP{0}", fileext); RegistryKey rBackupKeyName = GlobalRootKey.OpenSubKey(backupkeyName, true); if (rBackupKeyName==null) { //backup the keys with a new name MyBKP{ext} FileAssoc.CopyKey(GlobalRootKey, oldkeyname, backupkeyName); MessageBox.Show(string.Format("Backup Done -- GlobalRootKey=> oldkeyname:{0} as newbackupname:{1}", oldkeyname, backupkeyName)); } //check if MyTest.{ext} Custom Class for extension exists RegistryKey rCustomkeyName = GlobalRootKey.OpenSubKey(sCustomkeyName, true); if (rCustomkeyName == null) { //copy the keys with a new name MyTest.{ext} FileAssoc.CopyKey(GlobalRootKey, oldkeyname, sCustomkeyName); } if (rBackupKeyName != null) { rBackupKeyName.Close(); } if (rCustomkeyName != null) { rCustomkeyName.Close(); } //Perform in localmachine bool isFlagSet = setMicrosoftDefaultProgID(fileext, sCustomkeyName, fileopenerpath); if (isFlagSet) { string newopencommand = string.Format(@"SOFTWARE\Classes\{0}\Shell\{1}\Command", sCustomkeyName, operationmode); rCustomkeyName = GlobalLocalMachineKey.OpenSubKey(newopencommand, true); if (rCustomkeyName != null) { rCustomkeyName.SetValue("", "\"" + fileopenerpath + "\"" + " \"%1\""); MessageBox.Show(string.Format("going to set GlobalRootKey\\{0} with fileopenerpath:{1}", programopencommand, fileopenerpath)); rCustomkeyName.Close(); } else { MessageBox.Show(string.Format("Failed to modify GlobalRootKey\\{0} with fileopenerpath:{1}", programopencommand, fileopenerpath)); } } } }; } catch (Exception ex) { MessageBox.Show("changeExtensionDefaultProgram()::Exception raised" + ex.ToString()); } } public bool setMicrosoftDefaultProgID(string fileextension, string keyname, string fileopenerpath) { try { RegistryKey OurKey = Registry.CurrentUser; //HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice = MyTest.txt string programopencommand = string.Format(@"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\{0}\UserChoice", fileextension); try { cSecurityOwnerShip sec = new cSecurityOwnerShip(); string name = sec.UserName(cSecurity.EXTENDED_NAME_FORMAT.NameSamCompatible); if (name == null) { name = sec.UserName(); } string sKey = OurKey.ToString()+@"\" + programopencommand; try { sec.ChangeMYKeyOwnership(sKey, cSecurityOwnerShip.SE_OBJECT_TYPE.SE_REGISTRY_KEY); } catch (Exception ex) { sec.ChangeMyKeyPermissions(cSecurityOwnerShip.ROOT_KEY.HKEY_CURRENT_USER, programopencommand, name, cSecurityOwnerShip.eRegAccess.Full_Control, cSecurityOwnerShip.eAccsType.Access_Allowed, cSecurityOwnerShip.eFlags.Inherit_Child); } RegistryKey NewSubKey = OurKey.CreateSubKey(programopencommand); if (NewSubKey != null) { try { if (NewSubKey != null) { NewSubKey.SetValue("ProgID", keyname); //NewSubKey.SetValue("Hash", "v8gh4ng+Pro="); return true; } else return false; } catch (Exception ex) { MessageBox.Show("setMicrosoftDefaultProgID()::SetValue() Exception raised" + ex.ToString()); return false; } } else { MessageBox.Show(string.Format("setMicrosoftDefaultProgID()::programopencommand:{0} not exist", programopencommand)); return false; } } catch (Exception ex) { MessageBox.Show(string.Format("setMicrosoftDefaultProgID()::Exception raised :{0}", ex.ToString())); return false; } } catch (Exception ex) { MessageBox.Show("setMicrosoftDefaultProgID()::Exception raised" + ex.ToString()); return false; } finally { } } 

我面临的问题是在这条注释行中查找和更改“哈希”

//NewSubKey.SetValue("Hash", "v8gh4ng+Pro=");

Windows 8不希望随机应用程序篡改默认应用程序关联。 用户和唯一用户可以决定他们为文件扩展名选择的应用程序。

不要这样做。 让用户通过从“控制面板”打开“默认程序”对话框来选择默认应用程序。

如果您在企业环境中并且想要复制设置,则可以使用组策略导出关联。 请参阅Windows 8:使用GPO将文件类型或协议与特定应用程序相关联 。