I am trying to find ways to manage system storage efficiently.
One thing I've noticed while using MS OneDrive is that whenever I click "Free up space" on its context menu, it converts the "Storage on disk" to 0 bytes while its File size remains the same. I want to mimic what is happening over there.
Then I researched to see if I can partially modify its attribute (allocation size) through SetFileInformationByHandle
using FileAllocationInfo
as its parameter. It succeeded but both of its file size and allocation size became 0.
Test code as follows:
WCHAR wcsDebug[2048];
// testfile is 1224 bytes
HANDLE hFile = CreateFile(_TEXT("C:\\test\\testfile.txt"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
FILE_ALLOCATION_INFO allocInfo;
ZeroMemory(&allocInfo, sizeof(FILE_ALLOCATION_INFO));
BOOL fResult = SetFileInformationByHandle(hFile,
FileAllocationInfo,
&allocInfo,
sizeof(FILE_ALLOCATION_INFO));
// after the execution, the size becomes 0
if (fResult)
{
swprintf_s(wcsDebug, _countof(wcsDebug), L"SetFileInfomationByHandle() Success.");
OutputDebugString(wcsDebug);
}
else
{
swprintf_s(wcsDebug, _countof(wcsDebug), L"SetFileInfomationByHandle() Failed. Err(%08X)", GetLastError());
OutputDebugString(wcsDebug);
}
}
Any more ideas on developing the thoughts?