-
-
Save anonymous/53b81a190d0e24e93cbb00dd48ab5cc7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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