Esempio n. 1
0
 public void Dispose()
 {
     if (this._hFind != FileHelperWinAPI.INVALID_HANDLE_VALUE)
     {
         FileHelperWinAPI.FindClose(this._hFind); this._hFind = FileHelperWinAPI.INVALID_HANDLE_VALUE;
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Checks if a folder is Empty.  It will throw an exception if the folder does not exist.
        /// </summary>
        public static bool IsDirectoryEmpty_Fast(string path)
        {
            //Bad path throw exception
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(path);
            }

            //Get out if the folder is missing
            if (!Directory.Exists(path))
            {
                throw new DirectoryNotFoundException();
            }

            //Fix up the path for searching
            if (path.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                path += "*";
            }
            else
            {
                path += Path.DirectorySeparatorChar + "*";
            }

            //Data Instance.  This gets reused.
            WIN32_FIND_DATA findData;
            IntPtr          hFind = FileHelperWinAPI.INVALID_HANDLE_VALUE;

            try
            {
                //Find the first
                hFind = FileHelperWinAPI.FindFirstFile(path, out findData);
                if (hFind == FileHelperWinAPI.INVALID_HANDLE_VALUE)
                {
                    throw new Exception("Failed to get directory first file", Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()));
                }

                bool empty = true;
                do
                {
                    //Reject the . and .. directory entries.
                    if (findData.cFileName != "." && findData.cFileName != "..")
                    {
                        empty = false;
                    }
                }while (empty && FileHelperWinAPI.FindNextFile(hFind, out findData));

                return(empty);
            }
            catch (Exception e) { throw e; }
            finally { if (hFind != FileHelperWinAPI.INVALID_HANDLE_VALUE)
                      {
                          FileHelperWinAPI.FindClose(hFind);
                      }
            }
        }
Esempio n. 3
0
        public bool MoveNext()
        {
            bool result;

            if (this._current == null)
            {
                this._hFind = FileHelperWinAPI.FindFirstFile(this._rootFolder + "\\*", out this._findData);
                if (this._hFind == FileHelperWinAPI.INVALID_HANDLE_VALUE)
                {
                    throw new Exception("Failed to get first file in directory: " + this._rootFolder, Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()));
                }
                result = true;
            }
            else
            {
                result = FileHelperWinAPI.FindNextFile(this._hFind, out this._findData);
            }
            //Get out if there are no files
            if (!result)
            {
                this._current = null; return(result);
            }

            //Jump over self and parent pointers
            while (this._findData.cFileName == "." || this._findData.cFileName == "..")
            {
                result = FileHelperWinAPI.FindNextFile(this._hFind, out this._findData);
            }

            //Get out if there are no files
            if (!result)
            {
                this._current = null; return(result);
            }

            //Form the file or folder name
            var temp = Path.Combine(this._rootFolder, this._findData.cFileName);

            //Decide what the file system entry is
            switch (this._findData.dwFileAttributes)
            {
            case FileAttributes.FILE_ATTRIBUTE_DIRECTORY: { this._current = new DirectoryInfo(temp); break; }

            case FileAttributes.FILE_ATTRIBUTE_NORMAL: { this._current = new FileInfo(temp); break; }

            case FileAttributes.FILE_ATTRIBUTE_ARCHIVE: { this._current = new FileInfo(temp); break; }

            default: { throw new Exception("Can't determine if this is a folder or a file: " + temp); break; }
            }

            return(true);
        }
Esempio n. 4
0
        private static int CountItems_Fast(string path, uint filter, bool set)
        {
            int ret = 0;

            //Bad path throw exception
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(path);
            }

            //Get out if the folder is missing
            if (!Directory.Exists(path))
            {
                throw new DirectoryNotFoundException();
            }

            //Fix up the path for searching
            if (path.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                path += "*";
            }
            else
            {
                path += Path.DirectorySeparatorChar + "*";
            }

            //Data Instance.  This gets reused.
            WIN32_FIND_DATA findData;
            IntPtr          hFind = FileHelperWinAPI.INVALID_HANDLE_VALUE;

            try
            {
                //Find the first
                hFind = FileHelperWinAPI.FindFirstFile(path, out findData);
                if (hFind == FileHelperWinAPI.INVALID_HANDLE_VALUE)
                {
                    throw new Exception("Failed to get first file in directory", Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()));
                }

                do
                {
                    //Special directories
                    if (findData.cFileName == "." || findData.cFileName == "..")
                    {
                        continue;
                    }
                    //Filter
                    if (((findData.dwFileAttributes & filter) != 0) == set)
                    {
                        ret++;
                    }
                }while (FileHelperWinAPI.FindNextFile(hFind, out findData));
            }
            catch (Exception e) { throw e; }
            finally { if (hFind != FileHelperWinAPI.INVALID_HANDLE_VALUE)
                      {
                          FileHelperWinAPI.FindClose(hFind);
                      }
            }

            return(ret);
        }