public void Dispose() { if (this._hFind != FileHelperWinAPI.INVALID_HANDLE_VALUE) { FileHelperWinAPI.FindClose(this._hFind); this._hFind = FileHelperWinAPI.INVALID_HANDLE_VALUE; } }
/// <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); } } }
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); }