Y
Y
Yaroslav Yakovlev2022-03-05 21:06:50
C++ / C#
Yaroslav Yakovlev, 2022-03-05 21:06:50

How to find the folder I need by going through all the files on my computer in C #?

I need to go through all the files on my computer, excluding those folders that I don't have access to, and find the file I need by name or permission?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
oleg_ods, 2022-03-05
@yyakovlev0

The DirectoryInfo class has a GetDirectories() method that returns all subfolders and a GetFiles() method that returns all files from a folder. A file has a Name property that contains the name and extension of the file.
Write a method that looks for the desired file in a folder by name. If there is a match, return its full path, if not, call the method recursively for all subfolders.

#
#, 2022-03-06
@mindtester

on occasion)))
since it coincided, I give an example of an old, perhaps somewhat chaotic code

... keep in mind that this is an extension, not a method
.. да и вообще код сильно ориентирован на использование расшиений и функциональной парадигмы
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ff.links
{
    static partial class Program
    {
        static IEnumerable<string> scan(this IEnumerable<string> ds)
        {
            var files = new List<string>();
            foreach (var d in ds)
                try { files.add2my(d.here()).add2my(d.subdirs()); }
                catch (Exception e) { Console.WriteLine($"{pfx}Scan \"{d}\" - {e.Message}"); }
            return files;
        }
        static List<string> add2my(this List<string> l, IEnumerable<string> r) { l.AddRange(r); return l; }

        static IEnumerable<string> here(this string d) => Directory.EnumerateFiles(d).Where(f => f.isTarget());
        static IEnumerable<string> subdirs(this string d) => Directory.EnumerateDirectories(d).Where(p => !p.isIgnored()).scan();

        static void print(this string s, string pfx = "", string sfx = "") => Console.WriteLine(pfx + s + sfx);
        static void print(this IEnumerable<string> sa, string pfx = "", string sfx = "") => sa.ToList().ForEach(s => s.print(pfx, sfx));
        static bool isTarget(this string p) => targets.Contains(p.Split(backSlashDelimiter).Last());
        static string[] targets => new string[] { ffBinary, ffProfileSign, fflConfig };

        const string fflConfig = "ff.links.cfg.json";
        const string ffBinary = "firefox.exe";
        const string ffProfileSign = "compatibility.ini";
        const string skipd = ".default";

        static bool isIgnored(this string p) => ignored.Contains(p.Split(backSlashDelimiter, StringSplitOptions.RemoveEmptyEntries).Last());
        static string[] ignored => new string[]
        {
            "TorBrowser", "Microsoft", "MICROSOFT",  "WindowsApps",  "Windows", "WINDOWS",
            "ProgramData", "All Users", "Documents and Settings", //"Users",
            "My Documents", "My Pictures", "My Music", "My Videos", "Application Data",
            "Start Menu", "Local Settings", "Cookies", "NetHood", "PrintHood", "Recent", "SendTo", "Templates",
            "CrashReports", "WindowsImageBackup", "System Volume Information", "$Recycle.Bin", "$RECYCLE.BIN",
            "root", "Default User"
        };

        static char[] backSlashDelimiter = new char[] { backSlash };
        const char backSlash = '\\';

        static IEnumerable<string> fromRoot() => Environment.GetLogicalDrives().Where(p => !p.isIgnored());
        static IEnumerable<string> fromSysDrive() { yield return @"c:\"; }
        static IEnumerable<string> fromTypical()
        {
            var path = [email protected]"{Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)}\Mozilla Firefox";
            yield return path;
            int p;
            if ((p=path.IndexOf(" (x86)")) >= 0)
                yield return path = path.Remove(p, 6);
            path = [email protected]"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\Mozilla\Firefox\Profiles";
            yield return path;
        }
    }
}
well, whatever one may say, without an example of use, nowhere ..
using System;
using System.Diagnostics;
using System.Linq;

namespace ff.links
{
    static partial class Program
    {
        static void Main(string[] args)
        {
            var sw = new Stopwatch();
            sw.Start();
            "let's begin...".print();

            var finds = fromTypical().scan();
            "found targets is ".print(pfx, finds.Count().ToString());
            //finds.print();
            var bro = finds.Where(b => b.Contains(ffBinary));
            "found browsers is ".print(pfx, bro.Count().ToString());
            bro.print(pfx);
            var cfg = finds.Where(b => b.Contains(fflConfig));
            "found configs is ".print(pfx, cfg.Count().ToString());
            cfg.print(pfx);
            var profiles = finds.Where(b => (b.Contains(ffProfileSign) && !b.Contains(skipd)));
            "found profiles is ".print(pfx, profiles.Count().ToString());
            //profiles.print();

            profiles.buildLinks(bro.First());
            //profiles.prefsApplay();

            //links2start();
            sw.Stop();
            var ts = sw.Elapsed;
            $"RunTime {ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds:000}".print();
//#if DEBUG
//            "press any key to continue...".print();
//            Console.ReadKey();
//#endif
        }
        const string pfx = "  ::> ";
    }
}
example of searching the entire C drive:
var finds = @"C:\".scan();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question