Search code examples
c#windowsuwpfull-text-indexing

UWP-App created files not indexed in Windows 10 1803


In my UWP App I am creating files that should be indexed by the Windows-index so they can be found later using full-text search.

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;
}

In the example above, when executing the code under Windows 10, 1709, numberOfResults is 1. When executing the same code under Windows 10, 1803, numberOfResults is 0.

In both cases, the location is added to the Windows-index (added via "Indexing Options").

I checked the permissions and they appear to be exactly the same. I also tried to create a file manually in the respective folder and use the windows search of the Windows Explorer, it shows 0 results (under 1803, under 1709 the results are shown as expected). In a few cases, the created file ended up in the query and was searchable, I have no clue why.

I tried this under 3 different Windows 10, 1803 machines and the results are exactly the same (and on several 1709 machines, here it works fine).


Solution

  • I found the solution myself: Within the "LocalCache" and "LocalState" (and maybe also other app folders), a folder named "Indexed" is automatically added to the Windows search index. The folder has to be created directly below the "LocalCache" or "LocalState" folder.

    So by creating a folder named "Indexed" and putting files/folders into that folder, the files become indexed.

    See the working code below (works now in Windows 10, 1709 and 1803). I only changed the first line to create the "Indexed" folder.

    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;
    }
    

    To check whether the "Indexed" folder really is indexed, go to Indexing Options. The "Indexed" folder is always checked and the checkmark cannot be removed.

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