Example #1
0
        //public static void Delete(string path)
        //{
        //    DirectoryInfoEx dir;
        //    IStorage tempParentStorage = null;
        //    IntPtr tempParentStoragePtr = IntPtr.Zero;

        //    try
        //    {
        //        dir = new DirectoryInfoEx(Path.GetDirectoryName(path));
        //        string name = Path.GetFileName(path);
        //        IOTools.getIStorage(dir, out tempParentStoragePtr, out tempParentStorage);

        //        tempParentStorage.DestroyElement(name);
        //    }
        //    finally
        //    {
        //        if (tempParentStorage != null)
        //        {
        //            Marshal.ReleaseComObject(tempParentStorage);
        //            Marshal.Release(tempParentStoragePtr);
        //        }
        //    }
        //}

        /// <summary>
        /// Move directory or file, take full path of source and dest as parameter.
        /// </summary>
        public static void Move(string source, string dest)
        {
            DirectoryInfoEx fromDir, toDir;

            fromDir = new DirectoryInfoEx(Path.GetDirectoryName(source));
            toDir   = new DirectoryInfoEx(Path.GetDirectoryName(dest));
            if (fromDir.Equals(toDir))
            {
                Rename(source, Path.GetFileName(dest));
                return;
            }


            if (fromDir.Storage == null)
            {
                throw new IOException("Source directory does not support IStorage");
            }
            if (toDir.Storage == null)
            {
                throw new IOException("Destination directory does not support IStorage");
            }

            int hr = fromDir.Storage.MoveElementTo(Path.GetFileName(source), toDir.Storage,
                                                   Path.GetFileName(dest), ShellAPI.STGMOVE.MOVE);

            if (hr != ShellAPI.S_OK)
            {
                Marshal.ThrowExceptionForHR(hr);
            }
        }
Example #2
0
        //0.13: Added HasParent
        /// <summary>
        /// Return whether parent directory contain child directory.
        /// Aware Library, UserFiles and Public directory too.
        /// </summary>
        /// <param name="child"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static bool HasParent(FileSystemInfoEx child, DirectoryInfoEx parent)
        {
            if (parent == null)
            {
                //if (Debugger.IsAttached)
                //    Debugger.Break();
                return(false);
            }

            //::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Music.library-ms
            if (parent.FullName.StartsWith(IOTools.IID_Library) && parent.FullName.EndsWith(".library-ms"))
            {
                //Reverse
                foreach (DirectoryInfoEx subDir in parent.GetDirectories())
                {
                    if (subDir.Equals(child) || HasParent(child, subDir))
                    {
                        return(true);
                    }
                }
                return(false);
            }
            else
            {
                if (child.FullName.StartsWith(parent.FullName, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(true);
                }

                if (child.FullName.StartsWith(IID_UserFiles) || child.FullName.StartsWith(IID_Public))
                {
                    return(false);
                }
                FileSystemInfoEx current = child.Parent;
                while (current != null && !parent.Equals(current))
                {
                    current = current.Parent;
                }
                return(current != null);
            }
        }
Example #3
0
        //public static void Delete(string path)
        //{
        //    DirectoryInfoEx dir;
        //    IStorage tempParentStorage = null;
        //    IntPtr tempParentStoragePtr = IntPtr.Zero;
        //    try
        //    {
        //        dir = new DirectoryInfoEx(Path.GetDirectoryName(path));
        //        string name = Path.GetFileName(path);
        //        IOTools.getIStorage(dir, out tempParentStoragePtr, out tempParentStorage);
        //        tempParentStorage.DestroyElement(name);
        //    }
        //    finally
        //    {
        //        if (tempParentStorage != null)
        //        {
        //            Marshal.ReleaseComObject(tempParentStorage);
        //            Marshal.Release(tempParentStoragePtr);
        //        }
        //    }
        //}
        /// <summary>
        /// Move directory or file, take full path of source and dest as parameter.
        /// </summary>
        public static void Move(string source, string dest)
        {
            DirectoryInfoEx fromDir, toDir;

            fromDir = new DirectoryInfoEx(Path.GetDirectoryName(source));
            toDir = new DirectoryInfoEx(Path.GetDirectoryName(dest));
            if (fromDir.Equals(toDir))
            {
                Rename(source, Path.GetFileName(dest));
                return;
            }

            if (fromDir.Storage == null)
                throw new IOException("Source directory does not support IStorage");
            if (toDir.Storage == null)
                throw new IOException("Destination directory does not support IStorage");

            int hr = fromDir.Storage.MoveElementTo(Path.GetFileName(source), toDir.Storage,
                Path.GetFileName(dest), ShellAPI.STGMOVE.MOVE);

            if (hr != ShellAPI.S_OK)
                Marshal.ThrowExceptionForHR(hr);
        }
        public DirectoryTreeItemViewModel LookupChild(DirectoryInfoEx directory, Func<bool> cancelCheck)
        {
            if (cancelCheck != null && cancelCheck())
                return null;

            if (Parent != null)
                Parent.IsExpanded = true;
            if (directory == null)
                return null;

            if (!IsLoaded)
            {
                IsLoading = true;
                SubDirectories = getDirectories();
                HasSubDirectories = SubDirectories.Count > 0;
                IsLoading = false;
            }

            foreach (DirectoryTreeItemViewModel subDirModel in SubDirectories)
            {
                if (!subDirModel.Equals(dummyNode))
                {
                    DirectoryInfoEx subDir = subDirModel.EmbeddedDirectoryModel.EmbeddedDirectoryEntry;
                    if (directory.Equals(subDir))
                        return subDirModel;
                    else if (IOTools.HasParent(directory, subDir))
                        return subDirModel.LookupChild(directory, cancelCheck);
                }
            }
            return null;
        }
Example #5
0
        //0.13: Added HasParent
        /// <summary>
        /// Return whether parent directory contain child directory.
        /// Aware Library, UserFiles and Public directory too.
        /// </summary>
        /// <param name="child"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static bool HasParent(FileSystemInfoEx child, DirectoryInfoEx parent)
        {
            if (parent == null)
            {
                //if (Debugger.IsAttached)
                //    Debugger.Break();
                return false;
            }

            //::{031E4825-7B94-4DC3-B131-E946B44C8DD5}\Music.library-ms
            if (parent.FullName.StartsWith(IOTools.IID_Library) && parent.FullName.EndsWith(".library-ms"))
            {
                //Reverse
                foreach (DirectoryInfoEx subDir in parent.GetDirectories())
                    if (subDir.Equals(child) || HasParent(child, subDir))
                        return true;
                return false;
            }
            else
            {
                if (child.FullName.StartsWith(parent.FullName.TrimEnd('\\') + "\\", StringComparison.InvariantCultureIgnoreCase))
                    return true;

                if (child.FullName.StartsWith(IID_UserFiles) || child.FullName.StartsWith(IID_Public))
                    return false;
                FileSystemInfoEx current = child.Parent;
                while (current != null && !parent.Equals(current))
                    current = current.Parent;
                return (current != null);
            }
        }