locked
Monitor a folder in C++

    Question

  • I'm try to monitor a folder in metro c++

    Here is my code:

    StorageFolder^ folder=ApplicationData::Current->LocalFolder;

    auto query = folder->CreateFolderQuery();
    query->ContentsChanged += ref new TypedEventHandler<IStorageQueryResultBase^, Object^ >(this, &MainPage::Query_ContentsChange);

    void FileCallbackTest::MainPage::Query_ContentsChange(IStorageQueryResultBase^, Object^)
    {
    //do something...
    }

    but it still not work after I add an file to the local app folder

    There are a lot of sample code talkin about this issue in C#, but I need C++ sample.

    Could any one  tell me what's wrong of my code  ??

    Thanks a lot~



    • Edited by Gary1123 Monday, November 26, 2012 11:41 AM
    Monday, November 26, 2012 11:39 AM

Answers

  • Hi Gary,

    I can reproduce this issue (the code work for C# but not for C++) and reported it on the Connect site.

    https://connect.microsoft.com/VisualStudio/feedback/details/772698/msdn-forum-applicationdata-current-localfolder-createfolderquery-not-work-for-c

    Please use the above link to further monitor the status of this issue.


    Regards,
    Christian HL
    Microsoft Online Community Support


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    • Marked as answer by Jesse Jiang Monday, December 10, 2012 7:37 AM
    Thursday, November 29, 2012 9:59 AM
  • An additional note:  it appears the documentation for StorageFolderQueryResult.ContentsChanged has a bug.  It claims:

    Fires when a file is added to, deleted from, or modified in the folder being queried. This event only fires after GetFilesAsync has been called at least once.

    However, there is no GetFilesAsync member on StorageFolderQueryResult.  You need to call GetFoldersAsync on the query member.  This code worked for me:

    	StorageFolder^ folder = ApplicationData::Current->LocalFolder; 
    	static auto query = folder->CreateFolderQuery(); //need to keep the query object around after this function returns
    	query->ContentsChanged += ref new TypedEventHandler<IStorageQueryResultBase^, Object^ >(this, &MainPage::Query_ContentsChange); 
    	query->GetFoldersAsync();
    

    • Proposed as answer by Jesse Jiang Friday, December 7, 2012 1:47 AM
    • Marked as answer by Jesse Jiang Monday, December 10, 2012 7:34 AM
    Thursday, December 6, 2012 7:11 PM

All replies

  • Could this be the problem? From the StorageFolderQueryResult.ContectChanged documentation

          "This event only fires after GetFilesAsync has been called at least once."

    Monday, November 26, 2012 9:15 PM
  • I try to use GetFilesAsync after register event, but it still can not work.  

    Could any one give me a sample code to achieve this goal ?

    Thanks for any help.

    Tuesday, November 27, 2012 2:47 AM
  • Hi Gary,

    I can reproduce this issue (the code work for C# but not for C++) and reported it on the Connect site.

    https://connect.microsoft.com/VisualStudio/feedback/details/772698/msdn-forum-applicationdata-current-localfolder-createfolderquery-not-work-for-c

    Please use the above link to further monitor the status of this issue.


    Regards,
    Christian HL
    Microsoft Online Community Support


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    • Marked as answer by Jesse Jiang Monday, December 10, 2012 7:37 AM
    Thursday, November 29, 2012 9:59 AM
  • This behavior is expected.  Your 'query' variable appears to be a local.  So, it is going out of scope and being cleaned up at the end of that function.  So there is no object alive to call back Query_ContentsChange when you change the folder contents.  I was able to get this to work by making the 'query' local a static.  (Other solutions would be to make it global or make it a member variable of MainPage instead.)

    Thursday, December 6, 2012 1:31 AM
  • An additional note:  it appears the documentation for StorageFolderQueryResult.ContentsChanged has a bug.  It claims:

    Fires when a file is added to, deleted from, or modified in the folder being queried. This event only fires after GetFilesAsync has been called at least once.

    However, there is no GetFilesAsync member on StorageFolderQueryResult.  You need to call GetFoldersAsync on the query member.  This code worked for me:

    	StorageFolder^ folder = ApplicationData::Current->LocalFolder; 
    	static auto query = folder->CreateFolderQuery(); //need to keep the query object around after this function returns
    	query->ContentsChanged += ref new TypedEventHandler<IStorageQueryResultBase^, Object^ >(this, &MainPage::Query_ContentsChange); 
    	query->GetFoldersAsync();
    

    • Proposed as answer by Jesse Jiang Friday, December 7, 2012 1:47 AM
    • Marked as answer by Jesse Jiang Monday, December 10, 2012 7:34 AM
    Thursday, December 6, 2012 7:11 PM