NTFS sparse file support in .NET ?

  • Thread starter Thread starter DT
  • Start date Start date
D

DT

Does the .NET Framework have direct support for NTFS5 sparse files?
(something equivalent to Win32 DeviceIoControl with FSCTL_SET_PARSE)

For example, what's the .NET equivalent of this Win32 example:

HANDLE h = CreateFile("A huge file.txt",
GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, 0, NULL);
DWORD dw;
DeviceIoControl(h, FSCTL_SET_SPARSE, NULL,
0, NULL, 0, &dw, NULL);
LONG lDist = 8; // 32 GB!!!
SetFilePointer(h, 0, &lDist, FILE_BEGIN);
SetEndOfFile(h);
CloseHandle(h);

....or if I have to pinvoke the above Win32 from .NET, what's the .NET
declarations I need?

Thanks,

DT
 
DT said:
Does the .NET Framework have direct support for NTFS5 sparse files?
(something equivalent to Win32 DeviceIoControl with FSCTL_SET_PARSE)

For example, what's the .NET equivalent of this Win32 example:

HANDLE h = CreateFile("A huge file.txt",
GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, 0, NULL);
DWORD dw;
DeviceIoControl(h, FSCTL_SET_SPARSE, NULL,
0, NULL, 0, &dw, NULL);
LONG lDist = 8; // 32 GB!!!
SetFilePointer(h, 0, &lDist, FILE_BEGIN);
SetEndOfFile(h);
CloseHandle(h);

...or if I have to pinvoke the above Win32 from .NET, what's the .NET
declarations I need?

There is no direct support. You will have to pinvoke DeviceIoControl,
SetFilePointer, SetEndOfFile, CloseHandle, and CreateFile. FOr help on
pinvoke ask in the interop group.(note, you can create filestreams using the
handle returned from CreateFile)
 
Thanks, that's what I feared, so I've spent some time trying unsuccessfully
to invoke CreateFile from .NET, but always get invalid handle (-1) returned
from CreateFile.

So... I decided to try to create the file with FileStream and then use
fs.Handle to do the DeviceIoControl (code below).. But so far, also without
success.

I'll move over to the interop group for further questions...

DT

Public Const FSCTL_SET_SPARSE = 590020 ' &H000900C4
Dim fs As New FileStream(FILE_NAME, FileMode.CreateNew)

Dim h As IntPtr

h = fs.Handle

Dim iBytesReturned As Integer

DeviceIoControl(h, FSCTL_SET_SPARSE, Nothing, 0, Nothing, 0, iBytesReturned,
Nothing)
 
Back
Top