HOW to Create a SPARSE file in VB.NET

WESHILL
02-14-2004, 01:18 PM
Hi,

I am trying to create a SPARSE file that reflects the size of an original file. for excample, I would like to create a 33gb(Logical) file that is physically only a few bytes.

I am able to create a 0(logical/physical) byte file only with the following code but I can't seem to get the Logical value assigned. Could someone tel me what I'm missing?

I keep getting this message...
======================================================
System.NullReferenceException: Object reference not set to an instance of an object.
at WS_NTFS.Module1.SetFilePointer(Int32 hFile, Int32 lDistanceToMove, Int32 lpDistanceToMoveHigh, Int32 dwMoveMethod)
at WS_NTFS.Form1.Button1_Click(Object sender, EventArgs e) in D:\@Projects\WeshillNTFS\NTFS_5_PT2_Src\WS_NTFS\Form1.vb:line 133
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
======================================================

Module Module1

Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" ( _
ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
ByVal lpSecurityAttributes As Integer, _
ByVal dwCreationDisposition As Integer, _
ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As Integer

Public Declare Function SetFilePointer Lib "kernel32" Alias "SetFilePointer" ( _
ByVal hFile As Integer, _
ByVal lDistanceToMove As Integer, _
ByVal lpDistanceToMoveHigh As Integer, _
ByVal dwMoveMethod As Integer) As Integer

Public Declare Function SetEndOfFile Lib "kernel32" ( _
ByVal hFile As Integer) As Integer

Public Declare Function CloseHandle Lib "kernel32" ( _
ByVal hObject As Integer) As Integer

Public Declare Function DeviceIoControl Lib "kernel32" ( _
ByVal hDevice As Long, _
ByVal dwIoControlCode As Long, _
|ByVal lpInBuffer As String, _
ByVal nInBufferSize As Long, _
ByVal lpOutBuffer As String, _
ByVal nOutBufferSize As Long, _
ByVal lpBytesReturned As Long, _
ByVal lpOverlapped As String) As Long

End Module

Public Class Form1

Inherits System.Windows.Forms.Form

Const _WIN32_WINNT = 5
Public Const CREATE_ALWAYS = 2
Public Const CREATE_NEW = 1
Public Const OPEN_ALWAYS = 4 'Create if doesn't exist
Public Const OPEN_EXISTING = 3
Public Const TRUNCATE_EXISTING = 5
Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Const FSCTL_SET_SPARSE = &H900C4
Public Const MOVEFILE_REPLACE_EXISTING = &H1
Public Const FILE_ATTRIBUTE_TEMPORARY = &H100
Public Const FILE_BEGIN = 0
Public Const FILE_SHARE_READ = &H1
Public Const FILE_SHARE_WRITE = &H2
Public Const GENERIC_READ = &H80000000
Public Const GENERIC_WRITE = &H40000000


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim h
Dim d
Dim n
Dim dw As Integer
Dim lDist As Integer

h = CreateFile("D:\Wes.log", GENERIC_WRITE + GENERIC_READ, 0, 0, CREATE_ALWAYS, 0, 0)

dw = 0

d = DeviceIoControl(h, FSCTL_SET_SPARSE, n, 0, n, 0, dw, 0)

lDist = 8

MsgBox(h & " " & lDist)

SetFilePointer(h, 0, lDist, FILE_BEGIN)

CloseHandle(h)

End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum