Effective file search solution in UWP

123244 0 Reputation points
2024-07-25T15:52:39.7766667+00:00

Hi, I need to search for files based on their type, creation date, and size (something like in windows). What are the options for an efficient search?

I tried to use the Storage API, but the search is quite slow, in turn, System.IO can access folders to which the user does not have permission, so often the search is interrupted by “Access denied” (for example, System.IO can access “C:\System Volume Information”, which is not desirable, since the user does not have access to such system files).

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,096 questions
Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,642 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 61,491 Reputation points
    2024-07-25T17:56:45.54+00:00

    see Directory.EnumerateFiles. It has ignore no access files:

    var folder = @"c:\code";
    var match = "*.cs";
    var maxSize = 100;
    var filePaths = Directory.EnumerateFiles(folder, match, new EnumerationOptions
    {
        IgnoreInaccessible = true,
        RecurseSubdirectories = true
    }).Where(f =>
    {
        var fileInfo = new FileInfo(f);
        return fileInfo.Length < maxSize;
    });
    

    https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.enumeratefiles?view=net-8.0

    note: the windows file system is just not fast at searching for files.


0 additional answers

Sort by: Most helpful