PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/VBListFilesInDirectory/NativeMethods.vb

#
Visual Basic | 92 lines | 56 code | 13 blank | 23 comment | 0 complexity | 1461435676ff3fd0cb336314bec7203d MD5 | raw file
  1. '/************************************* Module Header **************************************\
  2. '* Module Name: NativeMethods.vb
  3. '* Project: VBListFilesInDirectory
  4. '* Copyright (c) Microsoft Corporation.
  5. '*
  6. '* The VBListFilesInDirectory project demonstrates how to implement an IEnumerable(Of 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. '* * 30/8/2009 1:00 PM Jie Wang Created
  16. '\******************************************************************************************/
  17. #Region "Using directives"
  18. Imports System
  19. Imports System.Runtime.InteropServices
  20. Imports System.Runtime.ConstrainedExecution
  21. Imports System.Security.Permissions
  22. Imports Microsoft.Win32.SafeHandles
  23. #End Region
  24. <Serializable(), StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto), _
  25. BestFitMapping(False)> _
  26. Friend Class WIN32_FIND_DATA
  27. Friend dwFileAttributes As Integer
  28. Friend ftCreationTime_dwLowDateTime As Integer
  29. Friend ftCreationTime_dwHighDateTime As Integer
  30. Friend ftLastAccessTime_dwLowDateTime As Integer
  31. Friend ftLastAccessTime_dwHighDateTime As Integer
  32. Friend ftLastWriteTime_dwLowDateTime As Integer
  33. Friend ftLastWriteTime_dwHighDateTime As Integer
  34. Friend nFileSizeHigh As Integer
  35. Friend nFileSizeLow As Integer
  36. Friend dwReserved0 As Integer
  37. Friend dwReserved1 As Integer
  38. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
  39. Friend cFileName As String
  40. <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=14)> _
  41. Friend cAlternateFileName As String
  42. End Class
  43. ''' <summary>
  44. ''' Win32 Native P/Invoke
  45. ''' </summary>
  46. Friend Module NativeMethods
  47. <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  48. Friend Function FindFirstFile( _
  49. ByVal fileName As String, _
  50. <[In](), Out()> ByVal data As WIN32_FIND_DATA) As SafeFindHandle
  51. End Function
  52. <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  53. Friend Function FindNextFile( _
  54. ByVal hndFindFile As SafeFindHandle, _
  55. <[In](), Out(), MarshalAs(UnmanagedType.LPStruct)> ByVal _
  56. lpFindFileData As WIN32_FIND_DATA) As Boolean
  57. End Function
  58. <ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll")> _
  59. Friend Function FindClose(ByVal handle As IntPtr) As Boolean
  60. End Function
  61. Friend Const ERROR_SUCCESS As Integer = 0
  62. Friend Const ERROR_NO_MORE_FILES As Integer = 18
  63. Friend Const ERROR_FILE_NOT_FOUND As Integer = 2
  64. Friend Const FILE_ATTRIBUTE_DIRECTORY As Integer = &H10
  65. End Module
  66. ''' <summary>
  67. ''' Safe handle for using with the Find File APIs.
  68. ''' </summary>
  69. Friend NotInheritable Class SafeFindHandle
  70. Inherits SafeHandleZeroOrMinusOneIsInvalid
  71. <SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode:=True)> _
  72. Friend Sub New()
  73. MyBase.New(True)
  74. End Sub
  75. Protected Overrides Function ReleaseHandle() As Boolean
  76. ' Close the search handle.
  77. Return NativeMethods.FindClose(MyBase.handle)
  78. End Function
  79. End Class