C# 代码在 Windows 8 中使用带有哈希的自定义程序更改附加了扩展的默认程序 [英] C# code to change the default program attached with an extension using a custom progid with hash in windows 8
问题描述
目的:将一个新的 progid 关联到一个扩展,这样文件就会用新的关联程序打开.
Purpose: Associate a new progid to an extension so that file will open with new associated program.
编程语言:C#
说明:
我想创建一个程序,将另一个程序与我推荐的程序列表中的扩展名相关联.我的程序在 windows-xp 和 windows-7 中运行,但在 windows-8 中不起作用.当我搜索该问题时,我发现在 Windows-8 中还有一个名为Hash"的附加键.
I want to create a program to associate another program with an extension from my recommended program list. My program is working in windows-xp and windows-7 but it is not working in windows-8. when i searched for the issue, i found that in Windows-8 there is an additional key called "Hash".
我找不到我的新 progid 的哈希值.
I am not able to find the hash for my new progid.
正在遵循的步骤:
在HKEY_CLASSES_ROOT
中创建了一个名为MyTest.txt"的类,例如:HKEY_CLASSES_ROOT MyTest.txt Shell 打开命令(默认)[PATH TO NOTEPAD]%1""
Created a class say "MyTest.txt" in HKEY_CLASSES_ROOT
eg: HKEY_CLASSES_ROOT MyTest.txt Shell Open Command (Default) "[PATH TO NOTEPAD] "%1""
我注意到在 LOCAL_MACHINE
文件夹中也创建了相同的密钥
I noticed that same key is also created in LOCAL_MACHINE
folder
现在我想将此MyTest.txt"ProgID 分配给
Now I want to assign this "MyTest.txt" ProgID to
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.txt\UserChoice]
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.txt\UserChoice]
"哈希"="??"
"ProgId"="MyTest.txt"
"ProgId"="MyTest.txt"
但是我无法在 C# 中找到我新创建的 ProgIdMyTest.txt"的哈希值.
But I am unable to find the Hash for my newly created ProgId "MyTest.txt" in C#.
使用 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
{
}
}
我面临的问题是在此注释行中查找和更改哈希"
Issue i am facing is in this commented line to find and change "Hash"
//NewSubKey.SetValue("Hash", "v8gh4ng+Pro=");
推荐答案
Windows 8 不希望随机应用程序篡改默认应用程序关联.用户和只有用户才能决定他们为文件扩展名选择的应用程序.
Windows 8 does not want random apps tampering with default application associations. Users and only users get to decide what application they choose for a file extension.
不要这样做.通过从控制面板打开默认程序"对话框,让用户选择默认应用程序.
Don't do this. Let the user choose default application by opening "Default Programs" dialog from Control Panel.
如果您在公司环境中并想要复制设置,您可以使用组策略导出关联.见 Windows 8:使用 GPO 将文件类型或协议与特定应用程序相关联.
If you're in a corporate environment and want to copy settings, you can export associations using group policies. See Windows 8: Associate a file Type or protocol with a specific app using GPO.
这篇关于C# 代码在 Windows 8 中使用带有哈希的自定义程序更改附加了扩展的默认程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!