N
nickdu
I'm trying to create a sparse file and set the EOF to 4 TB. I can do this
easily enough in unmanaged code:
#include <windows.h>
#include <stdio.h>
void main(int argc, char *argv[])
{
// Cleanup
HANDLE hFile = (HANDLE) INVALID_HANDLE_VALUE;
// Cleanup
LARGE_INTEGER li = {0, 1024};
DWORD cb;
DWORD dwTick;
HRESULT hr = S_OK;
dwTick = GetTickCount();
if ((hFile = CreateFile(TEXT("c:\\mapped.map"), GENERIC_READ |
GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_NO_BUFFERING, 0)) == (HANDLE) INVALID_HANDLE_VALUE)
{
hr = GetLastError();
hr = HRESULT_FROM_WIN32(hr);
goto cleanup;
}
printf("creating a sparse file.\n");
if (!DeviceIoControl(hFile, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &cb,
NULL))
{
hr = GetLastError();
hr = HRESULT_FROM_WIN32(hr);
goto cleanup;
}
if (!SetFilePointerEx(hFile, li, NULL, FILE_BEGIN))
{
hr = GetLastError();
hr = HRESULT_FROM_WIN32(hr);
printf("Failed SetFilePointerEx(), hr = 0x%08lX\n", hr);
goto cleanup;
}
if (!SetEndOfFile(hFile))
{
hr = GetLastError();
hr = HRESULT_FROM_WIN32(hr);
printf("Failed SetEndOfFile(), hr = 0x%08lX\n", hr);
goto cleanup;
}
cleanup:
if (hFile != (HANDLE) INVALID_HANDLE_VALUE)
CloseHandle(hFile);
dwTick = GetTickCount() - dwTick;
printf("Created file in %d milliseconds\n", dwTick);
return;
}
But I don't seem to be able to do the same thing using the .NET framework.
I tried:
using System;
using System.IO;
public class Application
{
public static void Main(string[] args)
{
if (args.Length != 2)
Console.WriteLine("Error, expecting file name and length.");
else
{
FileStream file = File.Create(args[0]);
file.Close();
File.SetAttributes(args[0], File.GetAttributes(args[0]) |
FileAttributes.SparseFile);
file = File.Open(args[0], FileMode.Open, FileAccess.ReadWrite,
FileShare.Read);
using(file)
{
file.SetLength(long.Parse(args[1]));
}
}
}
}
But it appears calling File.SetLength() is actually checking that I have
enough disk space and thus failing. Do I need to rely on interop?
--
Thanks,
Nick
(e-mail address removed)
remove "nospam" change community. to msn.com
easily enough in unmanaged code:
#include <windows.h>
#include <stdio.h>
void main(int argc, char *argv[])
{
// Cleanup
HANDLE hFile = (HANDLE) INVALID_HANDLE_VALUE;
// Cleanup
LARGE_INTEGER li = {0, 1024};
DWORD cb;
DWORD dwTick;
HRESULT hr = S_OK;
dwTick = GetTickCount();
if ((hFile = CreateFile(TEXT("c:\\mapped.map"), GENERIC_READ |
GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_NO_BUFFERING, 0)) == (HANDLE) INVALID_HANDLE_VALUE)
{
hr = GetLastError();
hr = HRESULT_FROM_WIN32(hr);
goto cleanup;
}
printf("creating a sparse file.\n");
if (!DeviceIoControl(hFile, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &cb,
NULL))
{
hr = GetLastError();
hr = HRESULT_FROM_WIN32(hr);
goto cleanup;
}
if (!SetFilePointerEx(hFile, li, NULL, FILE_BEGIN))
{
hr = GetLastError();
hr = HRESULT_FROM_WIN32(hr);
printf("Failed SetFilePointerEx(), hr = 0x%08lX\n", hr);
goto cleanup;
}
if (!SetEndOfFile(hFile))
{
hr = GetLastError();
hr = HRESULT_FROM_WIN32(hr);
printf("Failed SetEndOfFile(), hr = 0x%08lX\n", hr);
goto cleanup;
}
cleanup:
if (hFile != (HANDLE) INVALID_HANDLE_VALUE)
CloseHandle(hFile);
dwTick = GetTickCount() - dwTick;
printf("Created file in %d milliseconds\n", dwTick);
return;
}
But I don't seem to be able to do the same thing using the .NET framework.
I tried:
using System;
using System.IO;
public class Application
{
public static void Main(string[] args)
{
if (args.Length != 2)
Console.WriteLine("Error, expecting file name and length.");
else
{
FileStream file = File.Create(args[0]);
file.Close();
File.SetAttributes(args[0], File.GetAttributes(args[0]) |
FileAttributes.SparseFile);
file = File.Open(args[0], FileMode.Open, FileAccess.ReadWrite,
FileShare.Read);
using(file)
{
file.SetLength(long.Parse(args[1]));
}
}
}
}
But it appears calling File.SetLength() is actually checking that I have
enough disk space and thus failing. Do I need to rely on interop?
--
Thanks,
Nick
(e-mail address removed)
remove "nospam" change community. to msn.com