locked
directorychangesw-cannot rename file/folder in the monitored drive RRS feed

  • Question



  • Hi guys,
    I am using read directorychanges to get the changes for a drive.I am able to  monitor the changes.

    The problem is that I cannot rename a file\folder that I have created in the drive I monitor.

    Eg If I am monitoring D drive.

    In this case I can create D:\NewFolder
    But I cannot rename the NewFolder.When I try to do this I get the error "Folder is Use"

    But then I can create D:\NewFolder\NewFolder\
    which I can rename eg D:\NewFolder\MyNewName\

    The code I have given below


    HANDLE hDir = CreateFile("D:\\", FILE_LIST_DIRECTORY, FILE_SHARE_READ|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);


    while(true)
    {
        TCHAR szBuffer[640] = {0};
        dwOffset = 0;

        ReadDirectoryChangesW(hDir, szBuffer, sizeof(szBuffer) / sizeof(TCHAR), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_FILE_NAME, &dwBytes, NULL, NULL);
    do
    {
        // Get a pointer to the first change record...
        pInfo = (FILE_NOTIFY_INFORMATION*) &szBuffer[dwOffset];
       
    //save the created file name,Get the nextrecord
       
        dwOffset += pInfo->NextEntryOffset;

        }
    while(pInfo->NextEntryOffset != 0);




    What am I doing wrong.How can I avoid this?

    Cheers,
    Pat
    Sunday, March 23, 2008 1:39 AM

Answers

  •  patmorgan90 wrote:


    Hi guys,
    I am using read directorychanges to get the changes for a drive.I am able to  monitor the changes.

    The problem is that I cannot rename a file\folder that I have created in the drive I monitor.

    Eg If I am monitoring D drive.

    In this case I can create D:\NewFolder
    But I cannot rename the NewFolder.When I try to do this I get the error "Folder is Use"

    But then I can create D:\NewFolder\NewFolder\
    which I can rename eg D:\NewFolder\MyNewName\

    The code I have given below


    HANDLE hDir = CreateFile("D:\\", FILE_LIST_DIRECTORY, FILE_SHARE_READ|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);


    while(true)
    {
        TCHAR szBuffer[640] = {0};
        dwOffset = 0;

        ReadDirectoryChangesW(hDir, szBuffer, sizeof(szBuffer) / sizeof(TCHAR), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_FILE_NAME, &dwBytes, NULL, NULL);
    do
    {
        // Get a pointer to the first change record...
        pInfo = (FILE_NOTIFY_INFORMATION*) &szBuffer[dwOffset];
       
    //save the created file name,Get the nextrecord

       
        dwOffset += pInfo->NextEntryOffset;

        }
    while(pInfo->NextEntryOffset != 0);




    What am I doing wrong.How can I avoid this?

    Cheers,
    Pat

     

    hi, i think the problem is that u use createfile to get the handle of the directory. and the share mode parameter shows that other program only has permission to read or delete, no write operation is allowed.

     

    Monday, March 24, 2008 8:23 AM

All replies



  • Hi,
    Is everyone stumped or is this a stupid question to answer?
    Monday, March 24, 2008 8:11 AM
  •  patmorgan90 wrote:


    Hi guys,
    I am using read directorychanges to get the changes for a drive.I am able to  monitor the changes.

    The problem is that I cannot rename a file\folder that I have created in the drive I monitor.

    Eg If I am monitoring D drive.

    In this case I can create D:\NewFolder
    But I cannot rename the NewFolder.When I try to do this I get the error "Folder is Use"

    But then I can create D:\NewFolder\NewFolder\
    which I can rename eg D:\NewFolder\MyNewName\

    The code I have given below


    HANDLE hDir = CreateFile("D:\\", FILE_LIST_DIRECTORY, FILE_SHARE_READ|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);


    while(true)
    {
        TCHAR szBuffer[640] = {0};
        dwOffset = 0;

        ReadDirectoryChangesW(hDir, szBuffer, sizeof(szBuffer) / sizeof(TCHAR), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_FILE_NAME, &dwBytes, NULL, NULL);
    do
    {
        // Get a pointer to the first change record...
        pInfo = (FILE_NOTIFY_INFORMATION*) &szBuffer[dwOffset];
       
    //save the created file name,Get the nextrecord

       
        dwOffset += pInfo->NextEntryOffset;

        }
    while(pInfo->NextEntryOffset != 0);




    What am I doing wrong.How can I avoid this?

    Cheers,
    Pat

     

    hi, i think the problem is that u use createfile to get the handle of the directory. and the share mode parameter shows that other program only has permission to read or delete, no write operation is allowed.

     

    Monday, March 24, 2008 8:23 AM


  • Yep that was the problem.I didnt even notice that.Thanks.
    Cheers,
    Pat
    Monday, March 24, 2008 1:50 PM
  •  

    I'm facing a similar problem.

    I use ReadDirectoryChangesW() to monitor changes in a folder.

    I open the folder to monitor using

    CreateFileW() passing all the share flags i.e.,

    FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE

     

    I can't rename the folder that I'm monitoring (I guess it's because I have an open handle to that folder).

    Is there anyway I can get around this problem?

    TIA

     

    Tuesday, May 20, 2008 10:03 PM