コード例 #1
0
 /// <summary>
 /// Instantiates a new <see cref="HierarchicalFolder"/> with the given <paramref name="name"/> and <paramref name="rootPath"/>.
 /// </summary>
 /// <param name="parent">The parent folder this folder belongs to.</param>
 /// <param name="name">The name of the folder.</param>
 /// <param name="rootPath">The full root path for the folder.</param>
 /// <param name="isCompressed">Sets if this <see cref="HierarchicalFolder"/> represents a compressed folder.</param>
 public HierarchicalFolder(IHierarchicalFolder parent,
                           string name,
                           string rootPath,
                           bool isCompressed)
     : base()
 {
     Parent       = parent;
     Name         = name;
     Rootpath     = rootPath;
     IsCompressed = isCompressed;
 }
コード例 #2
0
        // --------

        /// <summary>
        /// Will convert an <see cref="IFileStore"/> into a list of hierarchical data suitably for binding to TreeViews.
        /// </summary>
        /// <param name="fileStore">The <see cref="IFileStore"/> to convert.</param>
        /// <param name="rootDirectoryName">The display name for the root directory. The root directory's RootPath is an empty string, this value will be used in place of that empty string.</param>
        /// <param name="folderSeparator">The string to use to separate folders.</param>
        /// <returns>A list of hierarchical data suitably for binding to TreeViews.</returns>
        public static List <IHierarchicalFolder> ConvertToHierarchicalList(IFileStore fileStore,
                                                                           string rootDirectoryName = "ROOT",
                                                                           string folderSeparator   = "\\")
        {
            if (fileStore == null)
            {
                throw new ArgumentNullException(nameof(fileStore));
            }
            var isCompressed = fileStore is CompressedFileStoreBase;
            var folders      = new List <IHierarchicalFolder>()
            {
                new HierarchicalFolder(null, rootDirectoryName, "", isCompressed)
            };

            foreach (var item in fileStore.GetAll())
            {
                AddFile(item);
            }
            return(folders);

            // ######## INTERNAL FUNCTIONS

            void AddFile(IFileStoreItem item_)
            {
                IHierarchicalFolder folder = null;

                // get the folder
                if (item_.Path == "")
                {
                    folder = folders[0];
                }
                else
                {
                    folder = GetFolderRecursive(folders[0], item_.Path, "");
                }

                // add file to folder
                folder.Files.Add(item_);
            }

            IHierarchicalFolder GetFolderRecursive(IHierarchicalFolder current_, string myPath_, string rootPath_)
            {
                // get path names
                var localDirName  = "";
                var remainingPath = "";

                if (myPath_.Contains(folderSeparator))
                {
                    localDirName  = myPath_.Substring(0, myPath_.IndexOf(folderSeparator));
                    remainingPath = myPath_.Substring(myPath_.IndexOf(folderSeparator) + 1);
                    rootPath_     = rootPath_ + folderSeparator + localDirName;
                }
                else
                {
                    localDirName = myPath_;
                    rootPath_    = rootPath_ + folderSeparator + localDirName;
                }
                // check for local directory
                IHierarchicalFolder foundFolder = null;

                foreach (var item in current_.Folders)
                {
                    if (item.Name.Equals(localDirName, StringComparison.InvariantCultureIgnoreCase))
                    {
                        foundFolder = item;
                        break;
                    }
                }
                // create local directory, if required
                if (foundFolder == null)
                {
                    var rp_ = rootPath_;
                    if (rp_.StartsWith("\\"))
                    {
                        rp_ = rp_.Substring(1);
                    }
                    foundFolder = new HierarchicalFolder(current_, localDirName, rp_, isCompressed);
                    current_.Folders.Add(foundFolder);
                }
                // check if done
                if (!string.IsNullOrWhiteSpace(remainingPath))
                {
                    // go again
                    foundFolder = GetFolderRecursive(foundFolder, remainingPath, rootPath_);
                }
                // done
                return(foundFolder);
            }
        }