这是为当前用户添加 Shell 文件夹的 C# 代码。如果您想为本地机器添加它,请将RegistryHive.CurrentUser 更改为RegistryHive.LocalMachine(在这种情况下您需要提升访问权限)。
添加外壳文件夹:
void fnCreateShellFolder(string strGUID, string strFolderTitle, string strTargetFolderPath, string strIconPath)
{
RegistryKey localKey, keyTemp, rootKey;
if (Environment.Is64BitOperatingSystem)
localKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
else
localKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);
rootKey = localKey.CreateSubKey(@"Software\Classes\CLSID\{" + strGUID + "}");
rootKey.SetValue("", strFolderTitle, RegistryValueKind.String);
rootKey.SetValue("System.IsPinnedToNameSpaceTree", unchecked((int)0x1), RegistryValueKind.DWord);
rootKey.SetValue("SortOrderIndex", unchecked((int)0x42), RegistryValueKind.DWord);
keyTemp = rootKey.CreateSubKey(@"DefaultIcon");
keyTemp.SetValue("", strIconPath, RegistryValueKind.ExpandString);
keyTemp.Close();
keyTemp = rootKey.CreateSubKey(@"InProcServer32");
keyTemp.SetValue("", @"%systemroot%\system32\shell32.dll", RegistryValueKind.ExpandString);
keyTemp.Close();
keyTemp = rootKey.CreateSubKey(@"Instance");
keyTemp.SetValue("CLSID", "{0E5AAE11-A475-4c5b-AB00-C66DE400274E}", RegistryValueKind.String);
keyTemp.Close();
keyTemp = rootKey.CreateSubKey(@"Instance\InitPropertyBag");
keyTemp.SetValue("Attributes", unchecked((int)0x11), RegistryValueKind.DWord);
keyTemp.SetValue("TargetFolderPath", strTargetFolderPath, RegistryValueKind.ExpandString);
keyTemp.Close();
keyTemp = rootKey.CreateSubKey(@"ShellFolder");
keyTemp.SetValue("FolderValueFlags", unchecked((int)0x28), RegistryValueKind.DWord);
keyTemp.SetValue("Attributes", unchecked((int)0xF080004D), RegistryValueKind.DWord);
keyTemp.Close();
rootKey.Close();
keyTemp = localKey.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\{" + strGUID + "}");
keyTemp.SetValue("", strFolderTitle, RegistryValueKind.String);
keyTemp.Close();
keyTemp = localKey.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel");
keyTemp.SetValue("{" + strGUID + "}", unchecked((int)0x1), RegistryValueKind.DWord);
keyTemp.Close();
}
删除 Shell 文件夹:
static void fnRemoveShellFolder(string strGUID)
{
RegistryKey localKey;
if (Environment.Is64BitOperatingSystem)
localKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
else
localKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);
localKey.DeleteSubKeyTree(@"Software\Classes\CLSID\{" + strGUID + "}", false);
localKey.DeleteSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\{" + strGUID + "}", false);
localKey.DeleteSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel", false);
}
您可能需要重新启动资源管理器才能看到更改。
// restart explorer
foreach (System.Diagnostics.Process exe in System.Diagnostics.Process.GetProcesses())
if (exe.ProcessName == "explorer")
exe.Kill();