Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文件夹固定到Windows资源管理器中的导航窗格_C#_Windows Explorer - Fatal编程技术网

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

C# 将文件夹固定到Windows资源管理器中的导航窗格,c#,windows-explorer,C#,Windows Explorer,我想创建一个文件夹并将其固定到Windows资源管理器中的导航窗格,但我不确定如何执行此操作 我想创建类似DropBox或OneDrive文件夹的东西 像这样的- 我看过这个问题,但我不认为这是我想要的。该应用程序将在Windows 7到Windows 10的计算机上运行。在所有这些操作系统上都可以这样做吗 任何帮助都将不胜感激。本文件部分记录了这一点:尽管标题具有误导性;它适用于常规文件夹。而且,它在Windows7、Windows8.x和Windows10上运行良好 下面是一个.BAT文件

我想创建一个文件夹并将其固定到
Windows资源管理器
中的
导航窗格
,但我不确定如何执行此操作

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

像这样的-

我看过这个问题,但我不认为这是我想要的。该应用程序将在Windows 7到Windows 10的计算机上运行。在所有这些操作系统上都可以这样做吗


任何帮助都将不胜感激。

本文件部分记录了这一点:尽管标题具有误导性;它适用于常规文件夹。而且,它在Windows7、Windows8.x和Windows10上运行良好

下面是一个.BAT文件的内容(使用标准的
reg.exe
工具,但您可以很容易地用C#code替换它),它可以创建一个类似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
注意:这将为当前用户注册文件夹(因此您不需要在注册表中拥有特殊权限),但如果您想为整个计算机注册文件夹,则必须将HKCU替换为HKLM(然后您需要适当的权限)

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

这是Windows 10上的外观:


要从窗格中删除该文件夹,请创建另一个包含以下内容的.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
下面是为当前用户添加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();
}
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);
}

要删除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();
}
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();

我只修改了一行Mangesh的代码,现在一切正常。因为当我调用Mangesh的fnRemoveShellFolder时,诸如“计算机”、“控制面板”和“用户”等桌面图标也会被删除。甚至他们也被“桌面图标设置”取消选中


可能的重复或可能的重复不是有意的重复,但感谢这两个似乎都为我指明了正确的方向,虽然我不一定是一条通往文件夹的捷径,但这是一种达到目的的手段!看起来您需要自定义Shell扩展。这个工作是C++而不是C语言,尽管在C语言中有一些材料是如何实现它的,这里有一个(或者谷歌的“iSeleFraseC**”)关于代码> Windows 10 < /C> >的任何想法?这对于<代码> OpenFieldLabe<代码>是不适用的(理想情况下,应该是这样)。是否有任何额外的条目?在8.1中,它仅适用于某些对话框。例如,如果我从Word打开对话框,它就会显示出来,但如果它是从VS打开的,它就不会显示。好的,是的,应用程序在调用这些对话框时可以使用标志。这些标志可以排除/包括文件夹,具体取决于文件夹的定义方式(例如,请参见FILEOPENDIALOGOPTIONS::FOS_FORCEFILESYSTEM或FOS_ALLNONSTORAGEITEMS)。您可以做的不多,但是您可以尝试使用ShellFolder\Attributes值映射来注意:这在Windows 7上不起作用。例如,dropbox甚至没有尝试在Windows 7上创建此图标。我想这些注册表设置有一些问题。当我调用您的FnRemoveShell文件夹时,桌面图标(如“计算机”、“控制面板”和“用户”)也会被删除。甚至它们也被“桌面图标设置”取消选中。这需要放入一个库中,作为外壳扩展名调用,以便用户可以右键单击任何文件夹并选择“锁定到导航”