/// <summary>Finds the <see cref="Win32FindData"/> with the specified path.</summary> /// /// <param name="path">The path of the file.</param> /// <param name="find">The output find data.</param> /// <returns>True if the file was found.</returns> private static bool GetData(string path, out Win32FindData find) { path = PathUtils.AddLongPathPrefix(path); find = new Win32FindData(); FindExFlags searchFlags; FindExInfoLevels infoLevel; if (SupportsExtraFindOptions) { searchFlags = FindExFlags.LargeFetch; infoLevel = FindExInfoLevels.Basic; } else { searchFlags = FindExFlags.None; infoLevel = FindExInfoLevels.Standard; } IntPtr hFind = FindFirstFileEx(path, infoLevel, out find, FindExSearchOps.NameMatch, IntPtr.Zero, searchFlags); try { return(hFind != InvalidHandle); } finally { FindClose(hFind); } }
/// <summary>Gets if the specified directory contains no files.</summary> /// /// <param name="directory">The path of the directory.</param> /// <returns>True if the directory contains no files.</returns> /// /// <exception cref="Win32Exception">Failed to search the directory.</exception> public static bool IsDirectoryEmpty(string directory) { string path = PathUtils.AddLongPathPrefix(Path.Combine(directory, "*")); Win32FindData find = new Win32FindData(); bool findResult = true; FindExFlags searchFlags; FindExInfoLevels infoLevel; if (SupportsExtraFindOptions) { searchFlags = FindExFlags.LargeFetch; infoLevel = FindExInfoLevels.Basic; } else { searchFlags = FindExFlags.None; infoLevel = FindExInfoLevels.Standard; } IntPtr hFind = FindFirstFileEx(path, infoLevel, out find, FindExSearchOps.NameMatch, IntPtr.Zero, searchFlags); if (hFind == InvalidHandle) { throw new Win32Exception(); } try { // Skip the '.' and '..' file entries. while (find.IsRelativeDirectory && findResult) { findResult = FindNextFile(hFind, out find); } return(!findResult); } finally { FindClose(hFind); } }