Skip to content

Instantly share code, notes, and snippets.

Created May 31, 2016 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/53b81a190d0e24e93cbb00dd48ab5cc7 to your computer and use it in GitHub Desktop.
Save anonymous/53b81a190d0e24e93cbb00dd48ab5cc7 to your computer and use it in GitHub Desktop.
#include "shlobj.h"
#include "ShObjIdl.h"
#include "Shlwapi.h"
int main(int, char **) {
// Initialize COM
CoInitialize(0);
// Get path to user's home directory
WCHAR path[MAX_PATH];
SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path);
// Contruct path to Box Sync folder
WCHAR boxSyncPath[MAX_PATH];
PathCombine(boxSyncPath, path, L"Box Sync");
// Contruct path to Box Sync\Documents folder
WCHAR boxSyncDocumentsPath[MAX_PATH];
PathCombine(boxSyncDocumentsPath, boxSyncPath, L"Documents");
// Set up a notification for when a change occurs in user's home directory
HANDLE change =
FindFirstChangeNotification(path, FALSE, FILE_NOTIFY_CHANGE_DIR_NAME);
while (true) {
// Wait for a change
WaitForSingleObject(change, INFINITE);
// Check if the Box Sync directory appeared
DWORD attributes = GetFileAttributes(boxSyncPath);
if (attributes != INVALID_FILE_ATTRIBUTES &&
(attributes & FILE_ATTRIBUTE_DIRECTORY)) {
// Create the KnownFolderManager object
IKnownFolderManager *folderManager = 0;
CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_ALL,
IID_IKnownFolderManager, (void **)&folderManager);
// Perform the redirection
LPWSTR error;
folderManager->Redirect(FOLDERID_Documents, NULL,
KF_REDIRECT_COPY_CONTENTS |
KF_REDIRECT_DEL_SOURCE_CONTENTS |
KF_REDIRECT_UNPIN | KF_REDIRECT_PIN,
boxSyncDocumentsPath, 0, NULL, &error);
return 0;
} else {
// Continue monitoring
FindNextChangeNotification(change);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment