File.Windows.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using Microsoft.Win32.SafeHandles;
  5. using System.IO;
  6. using System.Runtime.InteropServices;
  7. namespace Internal.IO
  8. {
  9. internal static partial class File
  10. {
  11. internal static bool InternalExists(string fullPath)
  12. {
  13. Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data = default;
  14. int errorCode = FillAttributeInfo(fullPath, ref data, returnErrorOnNotFound: true);
  15. return (errorCode == 0) && (data.dwFileAttributes != -1)
  16. && ((data.dwFileAttributes & Interop.Kernel32.FileAttributes.FILE_ATTRIBUTE_DIRECTORY) == 0);
  17. }
  18. /// <summary>
  19. /// Returns 0 on success, otherwise a Win32 error code. Note that
  20. /// classes should use -1 as the uninitialized state for dataInitialized.
  21. /// </summary>
  22. /// <param name="returnErrorOnNotFound">Return the error code for not found errors?</param>
  23. internal static int FillAttributeInfo(string path, ref Interop.Kernel32.WIN32_FILE_ATTRIBUTE_DATA data, bool returnErrorOnNotFound)
  24. {
  25. int errorCode = Interop.Errors.ERROR_SUCCESS;
  26. using (DisableMediaInsertionPrompt.Create())
  27. {
  28. if (!Interop.Kernel32.GetFileAttributesEx(path, Interop.Kernel32.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, ref data))
  29. {
  30. errorCode = Marshal.GetLastWin32Error();
  31. if (errorCode == Interop.Errors.ERROR_ACCESS_DENIED)
  32. {
  33. // Files that are marked for deletion will not let you GetFileAttributes,
  34. // ERROR_ACCESS_DENIED is given back without filling out the data struct.
  35. // FindFirstFile, however, will. Historically we always gave back attributes
  36. // for marked-for-deletion files.
  37. Interop.Kernel32.WIN32_FIND_DATA findData = default;
  38. using (SafeFindHandle handle = Interop.Kernel32.FindFirstFile(path, ref findData))
  39. {
  40. if (handle.IsInvalid)
  41. {
  42. errorCode = Marshal.GetLastWin32Error();
  43. }
  44. else
  45. {
  46. errorCode = Interop.Errors.ERROR_SUCCESS;
  47. data.PopulateFrom(ref findData);
  48. }
  49. }
  50. }
  51. }
  52. }
  53. if (errorCode != Interop.Errors.ERROR_SUCCESS && !returnErrorOnNotFound)
  54. {
  55. switch (errorCode)
  56. {
  57. case Interop.Errors.ERROR_FILE_NOT_FOUND:
  58. case Interop.Errors.ERROR_PATH_NOT_FOUND:
  59. case Interop.Errors.ERROR_NOT_READY: // Removable media not ready
  60. // Return default value for backward compatibility
  61. data.dwFileAttributes = -1;
  62. return Interop.Errors.ERROR_SUCCESS;
  63. }
  64. }
  65. return errorCode;
  66. }
  67. }
  68. }