且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何可以浏览在C#中的本地虚拟文件夹?

更新时间:2023-02-14 22:08:19

我知道这是疯狂的老,但如果有人想解决方案,这里是什么我在过去的半天寻找到这个想通了。有几种解决方案,有可以让你文件夹名称,如果你给它的路径,虚拟文件​​夹的XML地点,但没有我所看到的让你有从 :: {031E4825 - ......} 。还有在另外一个问题的回答使用WindowsAPICodePack的KnownFoldersBrowser例如提示。所以我通过源代码阅读并想出了以下内容:

I know this is crazy old, but in case someone wants the solution, here is what I have figured out over the past half-day looking into this. There are several solutions out there that can get you the folder names if you give it the path to the Virtual Folder's XML location , but nothing I have seen gets you there from ::{031E4825-....}. There was a hint in another question's answer to use the WindowsAPICodePack's KnownFoldersBrowser example. So I read through the source code in that and have come up with the following:

下面是我用得到的文件夹的DialogBox的,我已经启用了它AllowNonFileSystemItems ,这使得库文件夹选项:

Here is the DialogBox I was using to get folders, and I have enabled it for AllowNonFileSystemItems, which allows Library folder selections:

Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog dlg = new Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog();
dlg.Title = "Pick Folder";
dlg.IsFolderPicker = true;
dlg.InitialDirectory = Environment.SpecialFolder.Personal.ToString();  // If default setting does not exist, pick the Personal folder

dlg.AddToMostRecentlyUsedList = false;
dlg.AllowNonFileSystemItems = true;
dlg.DefaultDirectory = dlg.InitialDirectory;
dlg.EnsurePathExists = true;
dlg.EnsureFileExists = false;
dlg.EnsureReadOnly = false;
dlg.EnsureValidNames = true;
dlg.Multiselect = true;
dlg.ShowPlacesList = true;

if (dlg.ShowDialog() == Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogResult.Ok)
{
    foreach ( string dirname in dlg.FileNames )
    {
        var libFolders = ExpandFolderPath(dirname);
        if ( libFolders == null )
        {
            MessageBox.Show("Could not add '" + dirname + "', please try another.");
        }
        else
        {
            foreach ( string libfolder in libFolders )
            {
                DoWork(libfolder);
            }
        }
    }
}



我然后遍历 allSpecialFolders 找到同样的 :: {031E4825 -...} 这是ParsingName的SpecialFolder (是啊,可能是一个更优雅的方式)。在此之后,使用XML从其他解决方案读数(的我用做同样的事情一个CodeProject上的例子),以获得该库文件夹中的文件夹:

I then iterate over allSpecialFolders to find this same ::{031E4825-...} which is the ParsingName for the SpecialFolder (yeah, probably a more elegant way). After that, use the XML reading from other solutions (I used a CodeProject example that did the same thing) to get the folders in that library folder:

    /// <summary>Gets the folders associated with a path</summary>
    /// <param name="libname"></param>
    /// <returns>Folder, or List of folders in library, and null if there was an issue</string></returns>
    public List<string> ExpandFolderPath(string foldername)
    {
        List<string> dirList = new List<string> { };
        // If the foldername is an existing directory, just return that
        if ( System.IO.Directory.Exists(foldername) )
        {
            dirList.Add(foldername);
            return dirList;
        }

        // It's not a directory, so check if it's a GUID Library folder
        ICollection<IKnownFolder> allSpecialFolders = Microsoft.WindowsAPICodePack.Shell.KnownFolders.All;
        Regex libguid = new Regex(@"\b([A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-F0-9]{12})\b");
        var match = libguid.Match(foldername);
        if ( match == null )
            return null;

        string fpath = "";
        // Iterate over each folder and find the one we want
        foreach ( var folder in allSpecialFolders )
        {
            if ( folder.ParsingName == foldername )
            {
                // We now have access to the xml path
                fpath = folder.Path;
                break;
            }
        }
        if ( fpath == "" )
        {
            // Could not find it exactly, so find one with the same prefix, and
            // replace the filename
            foreach ( var folder in allSpecialFolders )
            {
                if ( folder.ParsingName.Contains(match.Groups[1].Value) )
                {
                    string sameDir = System.IO.Path.GetDirectoryName(folder.Path);
                    string newPath = System.IO.Path.Combine(sameDir, match.Groups[2].Value);
                    if ( System.IO.File.Exists(newPath) )
                        fpath = newPath;
                    break;
                }
            }
        }

        if ( fpath == "" )
            return null;

        var intFolders = GetLibraryInternalFolders(fpath);

        return intFolders.Folders.ToList();

    }


    /// <summary>
    /// Represents an instance of a Windows 7 Library
    /// </summary>
    public class Win7Library
    {
        public Win7Library()
        {

        }

        public string Name { get; set; }

        public string[] Folders { get; set; }
    }

    [DllImport("shell32.dll")]
    static extern int SHGetKnownFolderPath( [MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath );

    //Handles call to SHGetKnownFolderPath
    public static string getpathKnown( Guid rfid )
    {
        IntPtr pPath;
        if ( SHGetKnownFolderPath(rfid, 0, IntPtr.Zero, out pPath) == 0 )
        {
            string s = System.Runtime.InteropServices.Marshal.PtrToStringUni(pPath);
            System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pPath);

            return s;
        }
        else return string.Empty;
    }

    private static string ResolveStandardKnownFolders( string knowID )
    {
        if ( knowID.StartsWith("knownfolder:") )
        {
            return getpathKnown(new Guid(knowID.Substring(12)));
        }
        else
        {
            return knowID;
        }
    }

    private static Win7Library GetLibraryInternalFolders( string libraryXmlPath )
    {
        Win7Library newLibrary = new Win7Library();
        //The Name of a Library is just its file name without the extension
        newLibrary.Name = System.IO.Path.GetFileNameWithoutExtension(libraryXmlPath);

        List<string> folderpaths = new List<string>();

        System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); //* create an xml document object.
        xmlDoc.Load(libraryXmlPath); //* load the library as an xml doc.

        //Grab all the URL tags in the document, 
        //these point toward the folders contained in the library.
        System.Xml.XmlNodeList directories = xmlDoc.GetElementsByTagName("url");

        foreach ( System.Xml.XmlNode x in directories )
        {
            //Special folders use windows7 Know folders GUIDs instead 
            //of full file paths, so we have to resolve them
            folderpaths.Add(ResolveStandardKnownFolders(x.InnerText));
        }

        newLibrary.Folders = folderpaths.ToArray();
        return newLibrary;
    }



希望这可以帮助别人的未来!

Hope this helps someone in future!