nohairmonk
New member
- Joined
- Nov 28, 2020
- Messages
- 1
- Programming Experience
- Beginner
Hi,
I am trying use NtfsUsnJournal to count folders and files,I've modified codes,there have a question about return code. I want to return the value of foldersCount,how to do that?
The attachment is the full code.
I am trying use NtfsUsnJournal to count folders and files,I've modified codes,there have a question about return code. I want to return the value of foldersCount,how to do that?
C#:
public UsnJournalReturnCode
GetNtfsVolumeFolders(out List<Win32Api.UsnEntry> folders)
{
DateTime startTime = DateTime.Now;
folders = new List<Win32Api.UsnEntry>();
UsnJournalReturnCode usnRtnCode = UsnJournalReturnCode.VOLUME_NOT_NTFS;
Int32 FoldersCount = 0;
if (bNtfsVolume)
{
if (_usnJournalRootHandle.ToInt32() != Win32Api.INVALID_HANDLE_VALUE)
{
usnRtnCode = UsnJournalReturnCode.USN_JOURNAL_SUCCESS;
Win32Api.USN_JOURNAL_DATA usnState = new Win32Api.USN_JOURNAL_DATA();
usnRtnCode = QueryUsnJournal(ref usnState);
if (usnRtnCode == UsnJournalReturnCode.USN_JOURNAL_SUCCESS)
{
//
// set up MFT_ENUM_DATA structure
//
Win32Api.MFT_ENUM_DATA med;
med.StartFileReferenceNumber = 0;
med.LowUsn = 0;
med.HighUsn = usnState.NextUsn;
Int32 sizeMftEnumData = Marshal.SizeOf(med);
IntPtr medBuffer = Marshal.AllocHGlobal(sizeMftEnumData);
Win32Api.ZeroMemory(medBuffer, sizeMftEnumData);
Marshal.StructureToPtr(med, medBuffer, true);
//
// set up the data buffer which receives the USN_RECORD data
//
int pDataSize = sizeof(UInt64) + 10000;
IntPtr pData = Marshal.AllocHGlobal(pDataSize);
Win32Api.ZeroMemory(pData, pDataSize);
uint outBytesReturned = 0;
Win32Api.UsnEntry usnEntry = null;
//
// Gather up volume's directories
//
while (false != Win32Api.DeviceIoControl(
_usnJournalRootHandle,
Win32Api.FSCTL_ENUM_USN_DATA,
medBuffer,
sizeMftEnumData,
pData,
pDataSize,
out outBytesReturned,
IntPtr.Zero))
{
IntPtr pUsnRecord = new IntPtr(pData.ToInt32() + sizeof(Int64));
while (outBytesReturned > 60)
{
usnEntry = new Win32Api.UsnEntry(pUsnRecord);
//
// check for directory entries
//
if (usnEntry.IsFolder)
{
folders.Add(usnEntry);
FoldersCount += 1;
}
pUsnRecord = new IntPtr(pUsnRecord.ToInt32() + usnEntry.RecordLength);
outBytesReturned -= usnEntry.RecordLength;
}
Marshal.WriteInt64(medBuffer, Marshal.ReadInt64(pData, 0));
}
Marshal.FreeHGlobal(pData);
usnRtnCode = ConvertWin32ErrorToUsnError((Win32Api.GetLastErrorEnum)Marshal.GetLastWin32Error());
if (usnRtnCode == UsnJournalReturnCode.ERROR_HANDLE_EOF)
{
usnRtnCode = UsnJournalReturnCode.USN_JOURNAL_SUCCESS;
}
}
}
else
{
usnRtnCode = UsnJournalReturnCode.INVALID_HANDLE_VALUE;
}
}
folders.Sort();
System.Windows.MessageBox.Show(Convert.ToString(FoldersCount));
//return FoldersCount;
return usnRtnCode;
}
The attachment is the full code.
Attachments
Last edited by a moderator: