Skip to content

File System Helpers (filesystem.h)

Chris Guzak edited this page Jan 6, 2023 · 3 revisions

Folder Change Reader

Observe changes in a folder system including the specific changes using the Win32 API ReadDirectoryChangesW

#include <wil/filesystem.h>

auto reader = wil::make_folder_change_reader(folder.Path().c_str(), true, wil::FolderChangeEvents::All,
    [](wil::FolderChangeEvent event, PCWSTR fileName)
    {
         switch (event)
         {
         case wil::FolderChangeEvent::ChangesLost: break;
         case wil::FolderChangeEvent::Added:    break;
         case wil::FolderChangeEvent::Removed:  break;
         case wil::FolderChangeEvent::Modified: break;
         case wil::FolderChangeEvent::RenamedOldName: break;
         case wil::FolderChangeEvent::RenamedNewName: break;
     });

Folder Watcher

Observe changes to the file system using the Win32 API FindFirstChangeNotificationW

#include <wil/filesystem.h>

auto watcher = wil::make_folder_watcher(folder.Path().c_str(), true, wil::allChangeEvents, []()
    {
        // respond
    });

File Information

Get information about a file from an open handle.

auto fileBasicInfo = wil::GetFileInfo<FileBasicInfo>(fileHandle);

Create Directory

Create a directory including the intermediate path segments.

wil::CreateDirectoryDeepNoThrow(LR"(C:\temp\folder)");

Remove Directory

Delete a folder including its contents and sub-folders.

wil::RemoveDirectoryRecursive(LR"(c:\temp)", RemoveDirectoryOptions::RemoveReadOnly);