Windows 10 1803中创建的UWP应用程序文件未被索引

3
在我的UWP应用程序中,我正在创建文件,这些文件应该被Windows索引引用,以便稍后可以使用全文搜索找到它们。
public async Task TestFullTextSearch()
{
   StorageFolder folder = ApplicationData.Current.LocalCacheFolder;
   CreateFile(folder.Path + Path.DirectorySeparatorChar + "myDocument.txt", "Some text 123");
   await Task.Delay(5000); // to ensure that the file is already index before querying
   int numberOfResults = await SearchForResults(folder, "*");
   // numberOfResults is 1 in Windows 10, 1709 and 0 in 1803
}

public void CreateFile(string path, string text)
{
    using (StreamWriter sw = File.CreateText(path))
    {
        sw.WriteLine(text);
    }
}

private async Task<int> SearchForResults(StorageFolder folder, string searchString)
{
    QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderBySearchRank, new List<string>() { "*" });
    queryOptions.UserSearchFilter = searchString;
    StorageFileQueryResult queryResult = folder.CreateFileQueryWithOptions(queryOptions);
    IReadOnlyList<StorageFile> files = await queryResult.GetFilesAsync();
    return files.Count;
}

在上述示例中,在Windows 10版本1709下执行代码时,numberOfResults为1。在同样的代码在Windows 10版本1803下执行时,numberOfResults为0。
在这两种情况下,位置被添加到Windows索引(通过“索引选项”添加)。
我检查了权限,它们似乎完全相同。我还尝试在相应的文件夹中手动创建一个文件,并使用Windows资源管理器的Windows搜索功能,结果显示为0个(在1803下,结果如预期地显示为0)。在一些情况下,创建的文件最终出现在查询中并可搜索,我不知道为什么。
我在3台不同的Windows 10 1803机器上尝试过这个问题,结果完全相同(在几台1709机器上工作正常)。

你尝试在 Insider Hub 上报告这个问题了吗? - RoguePlanetoid
1
@RoguePlanetoid 感谢您的建议。反馈中心不允许我登录,所以我匿名创建了反馈。 - Rummy
好的,如果你需要我帮忙,请告诉我,如果你提供描述,我会为反馈点赞。 - RoguePlanetoid
1个回答

1
我自己找到了解决方案: 在“LocalCache”和“LocalState”(以及可能还有其他应用程序文件夹)中,会自动添加名为“Indexed”的文件夹到Windows搜索索引中。该文件夹必须直接创建在“LocalCache”或“LocalState”文件夹下面。
因此,通过创建一个名为“Indexed”的文件夹并将文件/文件夹放入其中,这些文件就会被索引。
请参见下面的可行代码(现在适用于Windows 10的1709和1803版本)。我只更改了第一行来创建“Indexed”文件夹。
public async Task TestFullTextSearch()
{
    StorageFolder folder = await ApplicationData.Current.LocalCacheFolder.CreateFolderAsync("Indexed", CreationCollisionOption.OpenIfExists);
    CreateFile(folder.Path + Path.DirectorySeparatorChar + "myDocument.txt", "Some text 123");
    await Task.Delay(5000); // to ensure that the file is already index before querying
    int numberOfResults = await SearchForResults(folder, "*");
    // numberOfResults is 1 in Windows 10, 1709 and 1 in 1803
}

public void CreateFile(string path, string text)
{
    using (StreamWriter sw = File.CreateText(path))
    {
        sw.WriteLine(text);
    }
}

private async Task<int> SearchForResults(StorageFolder folder, string searchString)
{
    QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderBySearchRank, new List<string>() { "*" });
    queryOptions.UserSearchFilter = searchString;
    StorageFileQueryResult queryResult = folder.CreateFileQueryWithOptions(queryOptions);
    IReadOnlyList<StorageFile> files = await queryResult.GetFilesAsync();
    return files.Count;
}

要检查“Indexed”文件夹是否真的已被索引,请转到索引选项。该“Indexed”文件夹始终被选中,无法取消勾选。

来源:https://learn.microsoft.com/en-us/uwp/api/windows.storage.applicationdata.localfolder


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接