PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 12ms 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
18
19#Region "Using directives"
20Imports System
21Imports System.Runtime.InteropServices
22Imports System.Runtime.ConstrainedExecution
23Imports System.Security.Permissions
24Imports Microsoft.Win32.SafeHandles
25#End Region
26
27
28<Serializable(), StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto), _
29BestFitMapping(False)> _
30Friend Class WIN32_FIND_DATA
31    Friend dwFileAttributes As Integer
32    Friend ftCreationTime_dwLowDateTime As Integer
33    Friend ftCreationTime_dwHighDateTime As Integer
34    Friend ftLastAccessTime_dwLowDateTime As Integer
35    Friend ftLastAccessTime_dwHighDateTime As Integer
36    Friend ftLastWriteTime_dwLowDateTime As Integer
37    Friend ftLastWriteTime_dwHighDateTime As Integer
38    Friend nFileSizeHigh As Integer
39    Friend nFileSizeLow As Integer
40    Friend dwReserved0 As Integer
41    Friend dwReserved1 As Integer
42    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
43    Friend cFileName As String
44    <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=14)> _
45    Friend cAlternateFileName As String
46End Class
47
48
49''' <summary>
50''' Win32 Native P/Invoke
51''' </summary>
52Friend Module NativeMethods
53    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
54    Friend Function FindFirstFile( _
55        ByVal fileName As String, _
56        <[In](), Out()> ByVal data As WIN32_FIND_DATA) As SafeFindHandle
57    End Function
58
59    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
60    Friend Function FindNextFile( _
61        ByVal hndFindFile As SafeFindHandle, _
62        <[In](), Out(), MarshalAs(UnmanagedType.LPStruct)> ByVal _
63         lpFindFileData As WIN32_FIND_DATA) As Boolean
64    End Function
65
66    <ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll")> _
67    Friend Function FindClose(ByVal handle As IntPtr) As Boolean
68    End Function
69
70    Friend Const ERROR_SUCCESS As Integer = 0
71    Friend Const ERROR_NO_MORE_FILES As Integer = 18
72    Friend Const ERROR_FILE_NOT_FOUND As Integer = 2
73    Friend Const FILE_ATTRIBUTE_DIRECTORY As Integer = &H10
74End Module
75
76
77''' <summary>
78''' Safe handle for using with the Find File APIs.
79''' </summary>
80Friend NotInheritable Class SafeFindHandle
81    Inherits SafeHandleZeroOrMinusOneIsInvalid
82
83    <SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode:=True)> _
84    Friend Sub New()
85        MyBase.New(True)
86    End Sub
87
88    Protected Overrides Function ReleaseHandle() As Boolean
89        ' Close the search handle.
90        Return NativeMethods.FindClose(MyBase.handle)
91    End Function
92End Class