NTFS sparse file support in .NET ?

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
 
D

Daniel O'Connell [C# MVP]

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)
 
D

DT

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top