在Windows资源管理器中将文件夹固定到导航窗格

时间:2014-05-21 08:18:15

标签: c# windows-explorer

我想创建一个文件夹并将其固定到Navigation Pane中的Windows Explorer,但我不确定如何执行此操作。

我想创建类似于DropBox或OneDrive文件夹的东西。

像这样 -

Something like this

我看过Manipulating the Windows 7 Explorer navigation pane问题,但我认为不是我想要的。该应用程序将在 Windows 7 Windows 10 的计算机上运行。是否可以在所有这些操作系统上执行此操作?

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:10)

本文件部分记录了这一点:Integrate a Cloud Storage Provider虽然标题具有误导性;它适用于常规文件夹。此外,它在Windows 7,Windows 8.x和10上运行良好。

因此,这里是.BAT文件的内容(使用标准reg.exe工具,但您可以轻松地用C#代码替换它),可以创建像OneDrive这样的文件夹。首先,你必须以任何方式创建一个Guid,用文件夹的显示名称替换“MyExt”名称,用任何物理路径替换“c:\ temp \ myext”路径,然后运行这个.bat文件。

reg add HKCU\Software\Classes\CLSID\{YOURGUID-GOES-HERE-0000-000000000000} /ve /t REG_SZ /d "MyExt" /f
reg add HKCU\Software\Classes\CLSID\{YOURGUID-GOES-HERE-0000-000000000000}\DefaultIcon /ve /t REG_EXPAND_SZ /d %%SystemRoot%%\system32\imageres.dll,-111 /f
reg add HKCU\Software\Classes\CLSID\{YOURGUID-GOES-HERE-0000-000000000000} /v System.IsPinnedToNameSpaceTree /t REG_DWORD /d 0x1 /f
reg add HKCU\Software\Classes\CLSID\{YOURGUID-GOES-HERE-0000-000000000000} /v SortOrderIndex /t REG_DWORD /d 0x42 /f
reg add HKCU\Software\Classes\CLSID\{YOURGUID-GOES-HERE-0000-000000000000}\InProcServer32 /ve /t REG_EXPAND_SZ /d %%systemroot%%\system32\shell32.dll /f
reg add HKCU\Software\Classes\CLSID\{YOURGUID-GOES-HERE-0000-000000000000}\Instance /v CLSID /t REG_SZ /d {0E5AAE11-A475-4c5b-AB00-C66DE400274E} /f
reg add HKCU\Software\Classes\CLSID\{YOURGUID-GOES-HERE-0000-000000000000}\Instance\InitPropertyBag /v Attributes /t REG_DWORD /d 0x11 /f
reg add HKCU\Software\Classes\CLSID\{YOURGUID-GOES-HERE-0000-000000000000}\Instance\InitPropertyBag /v TargetFolderPath /t REG_EXPAND_SZ /d "c:\temp\myext" /f
reg add HKCU\Software\Classes\CLSID\{YOURGUID-GOES-HERE-0000-000000000000}\ShellFolder /v FolderValueFlags /t REG_DWORD /d 0x28 /f
reg add HKCU\Software\Classes\CLSID\{YOURGUID-GOES-HERE-0000-000000000000}\ShellFolder /v Attributes /t REG_DWORD /d 0xF080004D /f
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\{YOURGUID-GOES-HERE-0000-000000000000} /ve /t REG_SZ /d "MyExt" /f
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel /v {YOURGUID-GOES-HERE-0000-000000000000} /t REG_DWORD /d 0x1 /f

请注意,这将注册当前用户的文件夹(因此您不需要注册表中的特殊权限),但如果您想为整个机器注册它,则必须由HKLM替换HKCU(和那么你需要适当的权利。

另请注意,我在imageres.dll中选择了一个默认图标,但您当然可以使用其他任何内容。

这就是它在Windows 10上的样子: enter image description here

要从窗格中删除该文件夹,请使用以下内容创建另一个.BAT文件:

reg delete HKCU\Software\Classes\CLSID\{YOURGUID-GOES-HERE-0000-000000000000}
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\{YOURGUID-GOES-HERE-0000-000000000000}
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel

答案 1 :(得分:6)

这是为当前用户添加Shell文件夹的C#代码。如果要将其添加到本地计算机,请将RegistryHive.CurrentUser更改为RegistryHive.LocalMachine(在这种情况下,您需要提升访问权限。)

添加Shell文件夹:

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();

答案 2 :(得分:4)

我改变了一行Mangesh的代码,现在一切正常。因为当我调用Mangesh的 fnRemoveShellFolder 时,桌面图标(如“计算机”,“控制面板”和“用户”)也会被删除。即使他们从“桌面图标设置”中取消选中。

localKey.DeleteSubKeyTree(@"Software\Classes\CLSID\{" + GUID + "}", false);
localKey.DeleteSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace\{" + GUID + "}", false);
tempKey = localKey.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel", true);
tempKey.DeleteValue("{" + GUID + "}", false);