PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSListFilesInDirectory/NativeMethods.cs

#
C# | 88 lines | 56 code | 9 blank | 23 comment | 0 complexity | fb1bc9db50810fcb3869d31b355f5246 MD5 | raw file
  1. /************************************* Module Header **************************************\
  2. * Module Name: NativeMethods.cs
  3. * Project: CSListFilesInDirectory
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * The CSListFilesInDirectory project demonstrates how to implement an IEnumerable<string>
  7. * that utilizes the Win32 File Management functions to enable application to get files and
  8. * sub-directories in a specified directory one item a time.
  9. *
  10. * This source is subject to the Microsoft Public License.
  11. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  12. * All other rights reserved.
  13. *
  14. * History:
  15. * * 7/7/2009 8:00 PM Jie Wang Created
  16. \******************************************************************************************/
  17. #region Using directives
  18. using System;
  19. using System.Runtime.InteropServices;
  20. using System.Runtime.ConstrainedExecution;
  21. using System.Security.Permissions;
  22. using Microsoft.Win32.SafeHandles;
  23. #endregion
  24. [Serializable, StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto),
  25. BestFitMapping(false)]
  26. internal class WIN32_FIND_DATA
  27. {
  28. internal int dwFileAttributes;
  29. internal int ftCreationTime_dwLowDateTime;
  30. internal int ftCreationTime_dwHighDateTime;
  31. internal int ftLastAccessTime_dwLowDateTime;
  32. internal int ftLastAccessTime_dwHighDateTime;
  33. internal int ftLastWriteTime_dwLowDateTime;
  34. internal int ftLastWriteTime_dwHighDateTime;
  35. internal int nFileSizeHigh;
  36. internal int nFileSizeLow;
  37. internal int dwReserved0;
  38. internal int dwReserved1;
  39. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  40. internal string cFileName;
  41. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
  42. internal string cAlternateFileName;
  43. }
  44. /// <summary>
  45. /// Win32 Native P/Invoke
  46. /// </summary>
  47. internal static class NativeMethods
  48. {
  49. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  50. internal static extern SafeFindHandle FindFirstFile(
  51. string fileName, [In, Out] WIN32_FIND_DATA data);
  52. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  53. internal static extern bool FindNextFile(
  54. SafeFindHandle hndFindFile,
  55. [In, Out, MarshalAs(UnmanagedType.LPStruct)]
  56. WIN32_FIND_DATA lpFindFileData);
  57. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll")]
  58. internal static extern bool FindClose(IntPtr handle);
  59. internal const int ERROR_SUCCESS = 0;
  60. internal const int ERROR_NO_MORE_FILES = 18;
  61. internal const int ERROR_FILE_NOT_FOUND = 2;
  62. internal const int FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
  63. }
  64. /// <summary>
  65. /// Safe handle for using with the Find File APIs.
  66. /// </summary>
  67. internal sealed class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
  68. {
  69. [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
  70. internal SafeFindHandle()
  71. : base(true)
  72. {
  73. }
  74. protected override bool ReleaseHandle()
  75. {
  76. // Close the search handle.
  77. return NativeMethods.FindClose(base.handle);
  78. }
  79. }