【发布时间】:2014-05-06 20:03:52
【问题描述】:
如何监控 AppData 文件夹中的特定文件。
我已经尝试使用 StorageFolderQueryResult.ContentsChanged 事件来处理这个问题,但这个事件实际上会回调文件夹中的任何更改。
我的问题是只监视单个文件并在其更改时使用事件处理程序。
我已尝试将此“UserSearchFilter”属性用于 QueryOptions。这实际上没有用。
有人帮忙吗?如果您可以另外提供整个问题的语法,那也会很有帮助。
我的 contentschanged 事件没有在修改文件夹中的“文件名”时触发。
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";
}
【问题讨论】:
标签: windows-8.1 winrt-async appdata