Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to list. Does it work differently?

Tags:

c#

.net

I have been using the library for a long time in my own goals to quickly find files on a PC - https://github.com/VladPVS/FastSearchLibrary

    public static string _keywords = "TestFile, .rar, .zip, .mp3, Bloody6, Artificial";        
    public void TestSe()
    {
        CancellationTokenSource tokenSource = new CancellationTokenSource();
        List<string> keywords = _keywords.Split(',').ToList(); // #2 <--------

        //List<string> keywords = new List<string>() {
        //    @"TestFile",
        //    @".rar",
        //    @".zip",
        //    @".mp3",
        //    @"Bloody6",
        //    @"Artificial" }; // #1 <----------

        List<string> folders = new List<string>();
        foreach (DriveInfo drive in DriveInfo.GetDrives())
        {
            if (drive.IsReady)
            {
                string driveRoot = drive.RootDirectory.FullName;
                folders.Add(driveRoot);
            }
        }
        searcher = new FileSearcherMultiple(folders, (f) =>
        {
            foreach (var keyword in keywords)
                if (f.Name.Contains(keyword))
                    return true;
            return false;
        }, tokenSource);

        List<FileInfo> files = new List<FileInfo>();

        searcher.FilesFound += (sndr, arg) =>
        {
            lock (locker)
            {
                arg.Files.ForEach((f) =>
                {
                    files.Add(f);

                    new Thread(() =>
                    {
                        //my work
                    }).Start();
                });
            }
        };

        searcher.SearchCompleted += (sndr, arg) =>
        {
            //ended
        };
        searcher.StartSearchAsync();
    }

I decided to display a list of keywords in the global string (as needed). But the search for some reason began to blunt. If you use the list directly as in #1, then it finds all files by keywords of 3000+ pieces. If you use #2, it finds 4-5 files by the keywords "Bloody6", "Artifical". What could be the problem?

like image 551
Xhonor Avatar asked Jan 20 '26 13:01

Xhonor


1 Answers

Most likely because if you used _keywords.Split(',') it will return extensions with spaces :

e.g. [space].rar, [space].zip, [space].mp3, etc.

You need to trim these values:

_keywords.Split(',').Select(s => s.TrimStart()).ToList();
like image 124
Kirill Polishchuk Avatar answered Jan 23 '26 02:01

Kirill Polishchuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!