File Speed Test 2

mail@pastecode.io avatar
unknown
csharp
a year ago
2.0 kB
0
Indexable
Never
// Get files with exclusions (string array defined in another class with user input)
public static string[] GetFilesExc(string path)
{
    var watch = new System.Diagnostics.Stopwatch();
    watch.Start();
    
    var fileList = new List<string>();

    foreach (var directoryPath in Directory.EnumerateDirectories(path))
    {
        try
        {
            var files = Directory.EnumerateFiles(directoryPath, "*", SearchOption.AllDirectories);

            foreach (var file in files)
            {
                string extension = Path.GetExtension(file);
                string parentDir = Path.GetDirectoryName(file);

                if (!BLEx.Contains(extension) || !BLDirs.Contains(directoryPath))
                {
                    fileList.Add(file);
                }
            }
        }
        catch (UnauthorizedAccessException)
        {
            continue;
        }
    }

    watch.Stop();
    Console.WriteLine($"[+] Grabbed files in {watch.ElapsedMilliseconds} ms");
    return fileList.ToArray();
}


// Get files by specific extension
public static string[] GetFilesByExt(string path, string extension)
{
    var watch = new System.Diagnostics.Stopwatch();
    watch.Start();
    
    var fileList = new List<string>();

    foreach (var directoryPath in Directory.EnumerateDirectories(path))
    {
        try
        {
            var files = Directory.EnumerateFiles(directoryPath, "*", SearchOption.AllDirectories);

            foreach (var file in files)
            {
                string fileExtension = Path.GetExtension(file);

                if (fileExtension == extension)
                {
                    fileList.Add(file);
                }
            }
        }
        catch (UnauthorizedAccessException)
        {
            continue;
        }
    }
    
    watch.Stop();
    Console.WriteLine($"[+] Grabbed files in {watch.ElapsedMilliseconds} ms");
    return fileList.ToArray();
}