PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms 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
18#region Using directives
19using System;
20using System.Runtime.InteropServices;
21using System.Runtime.ConstrainedExecution;
22using System.Security.Permissions;
23using Microsoft.Win32.SafeHandles;
24#endregion
25
26
27[Serializable, StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto), 
28BestFitMapping(false)]
29internal class WIN32_FIND_DATA
30{
31    internal int dwFileAttributes;
32    internal int ftCreationTime_dwLowDateTime;
33    internal int ftCreationTime_dwHighDateTime;
34    internal int ftLastAccessTime_dwLowDateTime;
35    internal int ftLastAccessTime_dwHighDateTime;
36    internal int ftLastWriteTime_dwLowDateTime;
37    internal int ftLastWriteTime_dwHighDateTime;
38    internal int nFileSizeHigh;
39    internal int nFileSizeLow;
40    internal int dwReserved0;
41    internal int dwReserved1;
42    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
43    internal string cFileName;
44    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
45    internal string cAlternateFileName;
46}
47
48/// <summary>
49/// Win32 Native P/Invoke
50/// </summary>
51internal static class NativeMethods
52{
53    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
54    internal static extern SafeFindHandle FindFirstFile(
55        string fileName, [In, Out] WIN32_FIND_DATA data);
56
57    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
58    internal static extern bool FindNextFile(
59        SafeFindHandle hndFindFile, 
60        [In, Out, MarshalAs(UnmanagedType.LPStruct)] 
61        WIN32_FIND_DATA lpFindFileData);
62
63    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll")]
64    internal static extern bool FindClose(IntPtr handle);
65
66    internal const int ERROR_SUCCESS = 0;
67    internal const int ERROR_NO_MORE_FILES = 18;
68    internal const int ERROR_FILE_NOT_FOUND = 2;
69    internal const int FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
70}
71
72/// <summary>
73/// Safe handle for using with the Find File APIs.
74/// </summary>
75internal sealed class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
76{
77    [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
78    internal SafeFindHandle()
79        : base(true)
80    {
81    }
82
83    protected override bool ReleaseHandle()
84    {
85        // Close the search handle.
86        return NativeMethods.FindClose(base.handle);
87    }
88}