1,488
社区成员




Public Class FileApi
Public Const INVALID_HANDLE_VALUE As Integer = -1
Public Enum DesiredAccess As UInt32
GENERIC_READ = &H80000000L
GENERIC_WRITE = &H40000000L
GENERIC_EXECUTE = &H20000000L
GENERIC_ALL = &H10000000L
End Enum
Public Enum ShareMode As UInt32
READ = 1
WRITE = 2
DELETE = 4
ALL = 7
End Enum
Public Enum CreationDisposition As UInt32
CREATE_NEW = 1
CREATE_ALWAYS = 2
OPEN_EXISTING = 3
OPEN_ALWAYS = 4
TRUNCATE_EXISTING = 5
End Enum
Public Enum FileAttribute
READ_ONLY = &H1
HIDDEN = &H2
SYSTEM = &H4
DIRECTORY = &H10
ARCHIVE = &H20
DEVICE = &H40
NORMAL = &H80
TEMPORARY = &H100
SPARSE_FILE = &H200
REPARSE_POINT = &H400
COMPRESSED = &H800
OFFLINE = &H1000
NOT_CONTENT_INDEXED = &H2000
ENCRYPTED = &H4000
End Enum
Public Structure SECURITY_ATTRIBUTES
Public Length As UInt32
Public SecurityDescriptor As Integer
Public bInheritHandle As Boolean
End Structure
Public Structure OVERLAPPED
Public Internal As Integer
Public InternalHigh As Integer
Public Offset As UInt32
Public OffsetHigh As UInt32
Public EventHandle As Integer
End Structure
Public Declare Auto Function CreateFile Lib "Kernel32.dll" (ByVal FileName As String, ByVal DesiredAccess As UInt32, ByVal ShareMode As UInt32, ByRef SecurityAttributes As SECURITY_ATTRIBUTES, ByVal CreationDisposition As UInt32, ByVal FlagsAndAttributes As UInt32, ByVal TemplateFile As Integer) As Integer
Public Declare Auto Function DeviceIoControl Lib "Kernel32.dll" (ByVal Handle As Integer, ByVal IoControlCode As UInt32, ByVal InBuffer() As Byte, ByVal InBufferSize As UInt32, ByVal OutBuffer() As Byte, ByVal OutBufferSize As UInt32, ByRef BytesReturned As UInt32, ByRef Overlapped As OVERLAPPED) As Boolean
Public Declare Auto Function WriteFile Lib "Kernel32.dll" (ByVal Handle As Integer, ByVal Bytes() As Byte, ByVal NumberOfBytesToWrite As UInt32, ByRef NumberOfBytesWritten As UInt32, ByVal Overlapped As OVERLAPPED) As Boolean
Public Declare Auto Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Integer) As Boolean
Public Shared Function CreateSparseFile(ByVal FileName As String) As Boolean
Dim handle As Integer
Dim bytes As UInt32
CreateSparseFile = False
handle = FileApi.CreateFile(FileName, FileApi.DesiredAccess.GENERIC_ALL, 0, Nothing, FileApi.CreationDisposition.CREATE_NEW, FileApi.FileAttribute.NORMAL, 0)
If handle <> FileApi.INVALID_HANDLE_VALUE Then
CreateSparseFile = FileApi.DeviceIoControl(handle, &H900C4, Nothing, 0, Nothing, 0, bytes, Nothing)
FileApi.CloseHandle(handle)
End If
End Function
End Class