Exemple #1
0
        private static FileAttributes loadAttributes(IShellFolder2 iShellFolder, PIDL pidlFull, PIDL pidlRel)
        {
            FileAttributes retVal = new FileAttributes();


            //ShellAPI.SFGAO attribute = shGetFileAttribute(pidlFull, ShellAPI.SFGAO.READONLY |
            //    ShellAPI.SFGAO.FOLDER | ShellAPI.SFGAO.FILESYSTEM | ShellAPI.SFGAO.STREAM | ShellAPI.SFGAO.FILESYSANCESTOR |
            //    ShellAPI.SFGAO.HIDDEN);
            ShellAPI.SFGAO attribute = ShellAPI.SFGAO.READONLY | ShellAPI.SFGAO.FOLDER | ShellAPI.SFGAO.FILESYSTEM | ShellAPI.SFGAO.STREAM | ShellAPI.SFGAO.FILESYSANCESTOR;
            iShellFolder.GetAttributesOf(1, new IntPtr[] { pidlRel.Ptr }, ref attribute);

            if (!IOTools.IsZip(attribute) && (attribute & ShellAPI.SFGAO.FOLDER) != 0)
            {
                retVal |= FileAttributes.Directory;
            }
            if ((attribute & ShellAPI.SFGAO.HIDDEN) != 0)
            {
                retVal |= FileAttributes.Hidden;
            }
            if ((attribute & ShellAPI.SFGAO.READONLY) != 0)
            {
                retVal |= FileAttributes.ReadOnly;
            }

            return(retVal);
        }
Exemple #2
0
        void handleEvent(WatcherChangeTypesEx changeType, bool isFolder, PIDL pidl)
        {
            if (IOTools.MatchFileMask(pidl, Filter))
            {
                FileSystemEventArgsEx args = new FileSystemEventArgsEx(changeType, isFolder,
                                                                       pidl, FileSystemInfoEx.PIDLToPath(pidl));
                switch (changeType)
                {
                case WatcherChangeTypesEx.Created: if (OnCreated != null)
                    {
                        OnCreated(this, args);
                    }
                    break;

                case WatcherChangeTypesEx.Changed: if (OnChanged != null)
                    {
                        OnChanged(this, args);
                    }
                    break;

                case WatcherChangeTypesEx.Deleted: if (OnDeleted != null)
                    {
                        OnDeleted(this, args);
                    }
                    break;
                }
            }
        }
        public IEnumerable <FileSystemInfoEx> EnumerateFileSystemInfos(String searchPattern, SearchOption searchOption, CancelDelegate cancel)
        {
            IEnumerator <DirectoryInfoEx> dirEnumerator = EnumerateDirectories(searchPattern, SearchOption.TopDirectoryOnly).GetEnumerator();

            while (!IOTools.IsCancelTriggered(cancel) && dirEnumerator.MoveNext())
            {
                yield return(dirEnumerator.Current);

                if (searchOption == SearchOption.AllDirectories)
                {
                    IEnumerator <FileSystemInfoEx> fsEnumerator =
                        dirEnumerator.Current.EnumerateFileSystemInfos(searchPattern, searchOption, cancel).GetEnumerator();
                    while (fsEnumerator.MoveNext())
                    {
                        yield return(fsEnumerator.Current);
                    }
                }
            }

            IEnumerator <FileInfoEx> fileEnumerator = EnumerateFiles(searchPattern, SearchOption.TopDirectoryOnly, cancel).GetEnumerator();

            while (!IOTools.IsCancelTriggered(cancel) && fileEnumerator.MoveNext())
            {
                yield return(fileEnumerator.Current);
            }
        }
Exemple #4
0
 public void MoveTo(string destFileName)
 {
     checkExists();
     destFileName = IOTools.ExpandPath(destFileName);
     IOTools.Move(FullName, destFileName);
     FullName     = destFileName;
     OriginalPath = FullName;
     Refresh();
 }
 /// <summary>
 /// Move this folder to specified directory (fullpath)
 /// </summary>
 public void MoveTo(string destDirName)
 {
     checkExists();
     //iStorage = null;
     //iShellFolder = null;
     destDirName = IOTools.ExpandPath(destDirName);
     IOTools.Move(FullName, destDirName);
     FullName     = destDirName;
     OriginalPath = FullName;
     Refresh();
 }
Exemple #6
0
 void handleRenameEvent(bool isFolder, PIDL pidl1, PIDL pidl2)
 {
     if (IOTools.MatchFileMask(pidl1, Filter))
     {
         if (OnRenamed != null)
         {
             OnRenamed(this, new RenameEventArgsEx(WatcherChangeTypesEx.Renamed, isFolder, pidl2,
                                                   FileSystemInfoEx.PIDLToPath(pidl2), FileSystemInfoEx.PIDLToPath(pidl1)));
         }
     }
 }
        //0.12: Fixed PIDL, PIDLRel, ShellFolder, Storage properties generated on demand to avoid x-thread issues.
        private Storage getStorage()
        {
            Storage iStorage = null;

            //0.15: Fixed ShellFolder not freed correctly
            //if (ShellFolder2 != null)
            if (IOTools.getIStorage(this, out iStorage))
            {
                return(iStorage);
            }
            return(null);
        }
Exemple #8
0
        public static void Copy(string source, string dest)
        {
            if (!FileEx.Exists(source))
            {
                throw new IOException("Source not exist.");
            }
            if (FileEx.Exists(dest))
            {
                throw new IOException("Dest already exist.");
            }

            IOTools.Copy(source, dest);
        }
        public DirectoryInfoEx(Environment.SpecialFolder shellFolder)
        {
            PIDL pidlLookup = CSIDLtoPIDL(IOTools.ShellFolderToCSIDL(shellFolder));

            try
            {
                init(pidlLookup);
                checkProperties();
            }
            finally
            {
                if (pidlLookup != null)
                {
                    pidlLookup.Free();
                }
                pidlLookup = null;
            }
        }
Exemple #10
0
        public DirectoryInfoEx(Environment.SpecialFolder shellFolder)
        {
            var sf = IOTools.ShellFolderToCSIDL(shellFolder);

            if (!sf.HasValue)
            {
                throw new ArgumentException("Cannot find CSIDL from this shell folder.");
            }
            PIDL pidlLookup = CSIDLtoPIDL(sf.Value);

            try
            {
                init(pidlLookup);
                checkProperties();
            }
            finally
            {
                //if (pidlLookup != null) pidlLookup.Free();
                pidlLookup = null;
            }
        }
        public static void Copy(string source, string dest)
        {
            if (!DirectoryEx.Exists(source))
            {
                throw new IOException("Source not exist.");
            }
            //if (DirectoryEx.Exists(dest))
            //    throw new IOException("Dest already exist.");

            IOTools.Copy(source, dest);

            string[] subFiles = GetFiles(source);
            foreach (string subFile in subFiles)
            {
                IOTools.Copy(subFile, subFile.Replace(source, dest));
            }

            string[] subDirs = GetDirectories(source);
            foreach (string subdir in subDirs)
            {
                Copy(subdir, subdir.Replace(source, dest));
            }
        }
Exemple #12
0
        protected virtual void init(string file, FileMode mode, FileAccess access)
        {
            _fileName = file;
            string          dirPath = Path.GetDirectoryName(file);
            DirectoryInfoEx dir     = new DirectoryInfoEx(dirPath);

            if (dir == null)
            {
                throw new IOException(String.Format("Directory not exists: {0}", dirPath));
            }
            if (!IOTools.getIStorage(dir, out tempParentStoragePtr, out tempParentStorage))
            {
                throw new IOException(String.Format("Failed to obtaiin IStorage: {0}", dirPath));
            }
            IOTools.openStream(tempParentStorage, file, ref mode, ref access, out streamPtr, out stream);

            this._canRead  = (access == FileAccess.Read || access == FileAccess.ReadWrite);
            this._canWrite = (access == FileAccess.Write || access == FileAccess.ReadWrite);
            currentPos     = 0;

            if (_canRead)
            {
                stream.Stat(out streamInfo, ShellAPI.STATFLAG.NONAME);
            }

            switch (mode)
            {
            case FileMode.Append:
                Seek(0, SeekOrigin.End);
                break;

            case FileMode.CreateNew:

                break;
            }
        }
 public static void Move(string sourceDirName, string destDirName)
 {
     IOTools.Move(sourceDirName, destDirName);
 }
        //0.17: Added DirectoryInfoEx.EnumerateFiles/EnumerateDirectories/EnumerateFileSystemInfos() methods which work similar as the one in .Net4
        public IEnumerable <FileInfoEx> EnumerateFiles(String searchPattern, SearchOption searchOption, CancelDelegate cancel)
        {
            IntPtr      ptrEnum    = IntPtr.Zero;
            IEnumIDList IEnum      = null;
            PIDL        parentPIDL = this.PIDL;

            using (ShellFolder2 sf = this.ShellFolder)
                try
                {
                    if (sf.EnumObjects(IntPtr.Zero, flag, out ptrEnum) == ShellAPI.S_OK)
                    {
                        IEnum = (IEnumIDList)Marshal.GetTypedObjectForIUnknown(ptrEnum, typeof(IEnumIDList));
                        IntPtr pidlSubItem;
                        int    celtFetched;

                        while (!IOTools.IsCancelTriggered(cancel) && IEnum.Next(1, out pidlSubItem, out celtFetched) == ShellAPI.S_OK && celtFetched == 1)
                        {
                            ShellAPI.SFGAO attribs = ShellAPI.SFGAO.FOLDER | ShellAPI.SFGAO.FILESYSTEM | ShellAPI.SFGAO.STREAM;
                            sf.GetAttributesOf(1, new IntPtr[] { pidlSubItem }, ref attribs);
                            //http://www.eggheadcafe.com/aspnet_answers/platformsdkshell/Mar2006/post26165601.asp
                            bool isZip = ((attribs & ShellAPI.SFGAO.FOLDER) != 0 && (attribs & ShellAPI.SFGAO.STREAM) != 0);
                            bool isDir = ((attribs & ShellAPI.SFGAO.FOLDER) != 0);
                            if (isZip || !isDir)
                            {
                                PIDL subRelPidl = new PIDL(pidlSubItem, false);
                                //FileInfoEx fi = new FileInfoEx(sf, this, subRelPidl);
                                FileInfoEx fi = new FileInfoEx(sf, parentPIDL, subRelPidl);
                                if (IOTools.MatchFileMask(fi.Name, searchPattern))
                                {
                                    yield return(fi);
                                }
                                //0.18: Fixed DirectoryInfoEx.EnumerateFiles, SearchPattern is ignored.
                            }
                        }

                        if (searchOption == SearchOption.AllDirectories)
                        {
                            IEnumerator <DirectoryInfoEx> dirEnumerator = EnumerateDirectories("*", SearchOption.TopDirectoryOnly, cancel).GetEnumerator();

                            while (!IOTools.IsCancelTriggered(cancel) && dirEnumerator.MoveNext())
                            {
                                IEnumerator <FileInfoEx> fileEnumerator = dirEnumerator.Current.EnumerateFiles(searchPattern, searchOption, cancel).GetEnumerator();

                                while (fileEnumerator.MoveNext())
                                {
                                    //Debug.Assert(!fileEnumerator.Current.IsFolder);
                                    yield return(fileEnumerator.Current);
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (parentPIDL != null)
                    {
                        parentPIDL.Free();
                        parentPIDL = null;
                    }

                    if (IEnum != null)
                    {
                        Marshal.ReleaseComObject(IEnum);
                        Marshal.Release(ptrEnum);
                    }
                }
        }
Exemple #15
0
 public FileInfoEx(string fullPath)
 {
     fullPath = IOTools.ExpandPath(fullPath);
     init(fullPath);
     checkProperties();
 }
Exemple #16
0
 public static void Move(string source, string dest)
 {
     IOTools.Move(source, dest);
     //new FileInfoEx(source).MoveTo(dest);
 }
        public IEnumerable <DirectoryInfoEx> EnumerateDirectories(String searchPattern, SearchOption searchOption, CancelDelegate cancel)
        {
            IntPtr      ptrEnum    = IntPtr.Zero;
            IEnumIDList IEnum      = null;
            PIDL        parentPIDL = this.PIDL;

            using (ShellFolder2 sf = this.ShellFolder)
                try
                {
                    if (sf.EnumObjects(IntPtr.Zero, flag, out ptrEnum) == ShellAPI.S_OK)
                    {
                        IEnum = (IEnumIDList)Marshal.GetTypedObjectForIUnknown(ptrEnum, typeof(IEnumIDList));
                        IntPtr pidlSubItem;
                        int    celtFetched;

                        while (!IOTools.IsCancelTriggered(cancel) && IEnum.Next(1, out pidlSubItem, out celtFetched) == ShellAPI.S_OK && celtFetched == 1)
                        {
                            ShellAPI.SFGAO attribs = ShellAPI.SFGAO.FOLDER | ShellAPI.SFGAO.FILESYSTEM | ShellAPI.SFGAO.STREAM |
                                                     ShellAPI.SFGAO.FILESYSANCESTOR | ShellAPI.SFGAO.NONENUMERATED;
                            sf.GetAttributesOf(1, new IntPtr[] { pidlSubItem }, ref attribs);
                            bool isZip = ((attribs & ShellAPI.SFGAO.FOLDER) != 0 && (attribs & ShellAPI.SFGAO.STREAM) != 0);
                            bool isDir = ((attribs & ShellAPI.SFGAO.FOLDER) != 0);
                            //0.18 Added a check for NonEnumerated items so DirectoryInfoEx.EnumerateDirectories wont return some system directories (e.g. C:\MSOCache)
                            //bool isNonEnumerated = ((attribs & ShellAPI.SFGAO.NONENUMERATED) != 0);
                            bool isFileAncestor = ((attribs & ShellAPI.SFGAO.FILESYSANCESTOR) != 0);
                            bool includedFolder = false;

                            if (!isZip && !isFileAncestor) //0.14 : Added allowed folder list so Non-FileAncestor directory (e.g. recycle-bin) is listed.
                            {
                                string[] allowedPaths = new string[]
                                {
                                    "::{645FF040-5081-101B-9F08-00AA002F954E}"
                                };
                                string path = PIDLToPath(new PIDL(pidlSubItem, false));
                                foreach (string allowedPath in allowedPaths)
                                {
                                    if (allowedPath == path)
                                    {
                                        includedFolder = true;
                                    }
                                }
                                if (!includedFolder)
                                {
                                    if (IOTools.HasParent(this, NetworkDirectory))
                                    {
                                        includedFolder = true;
                                    }
                                }
                            }
                            if (isDir && !isZip /*&& !isNonEnumerated*/ && (isFileAncestor || includedFolder))
                            {
                                PIDL subPidl = new PIDL(pidlSubItem, false);
                                //DirectoryInfoEx di = new DirectoryInfoEx(this, subPidl);

                                //0.22: Fix illegal PIDL for Directory under Library.ms directory
                                bool            isLibraryItem = IOTools.IsLibraryItem(FullName);
                                DirectoryInfoEx di            = new DirectoryInfoEx(sf, parentPIDL, subPidl, isLibraryItem);

                                if (IOTools.MatchFileMask(di.Name, searchPattern))
                                {
                                    yield return(di);
                                }
                                if (searchOption == SearchOption.AllDirectories)
                                {
                                    IEnumerator <DirectoryInfoEx> dirEnumerator = di.EnumerateDirectories(searchPattern, searchOption, cancel).GetEnumerator();

                                    while (dirEnumerator.MoveNext())
                                    {
                                        //Debug.Assert(dirEnumerator.Current.IsFolder);
                                        yield return(dirEnumerator.Current);
                                    }
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (parentPIDL != null)
                    {
                        parentPIDL.Free();
                        parentPIDL = null;
                    }

                    if (IEnum != null)
                    {
                        Marshal.ReleaseComObject(IEnum);
                        Marshal.Release(ptrEnum);
                    }
                }
        }