locked
Monitor a file in AppData (Local store), ContentsChanged not firing

    Question

  • Hi,

    how would a monitor just a particular file in AppData folder.

    I've tried using StorageFolderQueryResult.ContentsChanged event to handle this, but this one actually callbacks for any changes through the folder.

    My problem is to just a monitor a single file and use the eventhandler when its changed.

    I've tried to use this "UserSearchFilter" property to QueryOptions. That didnt work actually.

    cAn someone help with this ? It would also be helpful if you could additionally provide the syntax for the whole problem.

    My contentschanged event is not firing on modifying the "filename" in the folder.

        auto fileTypeFilter = ref new Platform::Collections::Vector<Platform::String^>();
        fileTypeFilter->Append("*");

        auto queryOptions = ref new QueryOptions(CommonFileQuery::OrderBySearchRank, fileTypeFilter);
        //use the user's input to make a query
        queryOptions->UserSearchFilter = InputTextBox->Text;
        auto queryResult = musicFolder->CreateFileQueryWithOptions(queryOptions);


    auto localFolder = ApplicationData::Current->LocalFolder;
    auto currPath = localFolder->Path;
    auto fileTypeFilter = ref new Platform::Collections::Vector<Platform::String^>();
    fileTypeFilter->Append(".dat");

    auto queryOptions = ref new QueryOptions(CommonFileQuery::OrderByName, fileTypeFilter);
    //use the user's input to make a query
    queryOptions->UserSearchFilter = L"filename";
    auto queryResult = localFolder->CreateFileQueryWithOptions(queryOptions);

    queryResult->ContentsChanged += ref new TypedEventHandler<IStorageQueryResultBase^, Platform::Object^>(this, &Scenario1::OnLocalAppDataChanged);

        //find all files that match the query
        create_task(queryResult->GetFilesAsync()).then([this, queryOptions](IVectorView<StorageFile^>^ files)
        {
            String^ outputText = "";

            //output how many files that match the query were found
            if (files->Size == 0)
            {
                outputText += "No files found for '" + queryOptions->UserSearchFilter + "'";
            }
            else if (files->Size == 1)
            {
                outputText += files->Size.ToString() + " file found:\n\n";
            }
            else
            {
                outputText += files->Size.ToString() + " files found:\n\n";
            }

            //output the name of each file that matches the query
            for (unsigned int i = 0; i < files->Size; i++)
            {
                outputText += files->GetAt(i)->Name + "\n";
            }

            OutputTextBlock->Text = outputText;
        });



    }

    void Scenario1::OnLocalAppDataChanged(Windows::Storage::Search::IStorageQueryResultBase^ sender, Platform::Object^ args)
    {
    Platform::String^ hello = L"hello world, I'm called back";
    }
    • Edited by Minu7 Tuesday, May 6, 2014 2:29 PM added code
    Tuesday, May 6, 2014 1:42 PM

All replies

  • Hi,

    Your code seems right. And When I create a project to test it and also found the ContentsChanged not firing. That's may be a bug. And you can use this code to instead:

    MainPage::MainPage()
    {
    	
    	InitializeComponent();
    	
    	
    	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<Windows::Storage::Search::IStorageQueryResultBase^, Platform::Object^>(this, &MainPage::OnLocalAppDataChanged);
    	query->GetFoldersAsync();
    
    		
    }
    void MainPage::OnLocalAppDataChanged(Windows::Storage::Search::IStorageQueryResultBase^ sender, Platform::Object^ args)
    {
    	
    }
    
    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, May 7, 2014 4:35 AM
  • Thanks for that. Should I have to register this event handler even in suspended state ? How to keep that registered ?
    Wednesday, May 7, 2014 5:32 AM
  • Hi,

    You should register the event handle in the suspending event. So the link below:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.search.storagefolderqueryresult.contentschanged.aspx

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, May 8, 2014 7:21 AM
  • CAn you please provide me the syntax to do it ? I have the contents changed handler registered once inside a lambda body, in a different page's method.

    Should i have to re-register it again once in the suspending event ? CAn you please provide me a small example for this ? Currently, I have my handler registered like this.

    StorageFolder^ AppDataLocal = ApplicationData::Current->LocalFolder;
    auto localPath = AppDataLocal->Path;
    auto subFolderPath = localPath + L"\\subfolder";

    //obtain static reference to the sub folder containing hints in AppData Local
    StorageFolder^ hintFolder;

    Concurrency::create_task(AppDataLocal->GetFolderAsync("subfolder")).then([this](StorageFolder^ hintSubFolder)
    //create_task(AppDataLocal->GetFolderFromPathAsync(subFolderPath)).then([](StorageFolder^ hintSubFolder)
    {
    if (hintSubFolder)
    {

    String^ hintPath = hintSubFolder->Path;
    auto QueryLocalOptions = ref new QueryOptions();
    static auto localQueryResult = hintSubFolder->CreateFolderQuery();


    localQueryResult->ContentsChanged += ref new TypedEventHandler<IStorageQueryResultBase^, Platform::Object^>(this, &scenario1::OnLocalAppDataChanged);


    //mandatory to call this atleast once, allows handler to hook on to the events
    localQueryResult->GetFoldersAsync();
    }
    else
    {
    //handle this
    }
    });

    Thursday, May 8, 2014 7:53 AM