Created
March 31, 2024 18:08
-
-
Save MzHmO/7da6f9a12c7fe735e614bce9cd6602fb to your computer and use it in GitHub Desktop.
Stats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Reflection; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
class AssemblyStatistics | |
{ | |
static void Main() | |
{ | |
string[] systemDirectories = | |
{ | |
"C:\\Windows\\", | |
"C:\\Program Files\\", | |
"C:\\ProgramData\\", | |
"C:\\Program Files (x86)\\" | |
}; | |
var fileStatistics = new FileStatistics(); | |
const int MaxDepth = 5; | |
foreach (string directoryPath in systemDirectories) | |
{ | |
if (!Directory.Exists(directoryPath)) | |
{ | |
Console.WriteLine($"Directory {directoryPath} does not exist or is not accessible."); | |
continue; | |
} | |
var files = new List<string>(); | |
GatherFiles(directoryPath, files, 1, MaxDepth); | |
Parallel.ForEach(files, file => | |
{ | |
ProcessFile(file, fileStatistics); | |
}); | |
} | |
fileStatistics.ReportStatistics(); | |
} | |
static void ProcessFile(string file, FileStatistics fileStatistics) | |
{ | |
string extension = Path.GetExtension(file).ToLower(); | |
try | |
{ | |
if (extension == ".exe" || extension == ".dll") | |
{ | |
AssemblyName.GetAssemblyName(file); | |
lock (fileStatistics) | |
{ | |
fileStatistics.IncrementValid(extension); | |
} | |
} | |
} | |
catch (Exception) | |
{ | |
} | |
lock (fileStatistics) | |
{ | |
fileStatistics.IncrementTotal(extension); | |
} | |
} | |
static void GatherFiles(string path, List<string> files, int currentDepth, int maxDepth) | |
{ | |
if (currentDepth > maxDepth) return; | |
try | |
{ | |
files.AddRange(Directory.GetFiles(path)); | |
foreach (var directory in Directory.GetDirectories(path)) | |
{ | |
GatherFiles(directory, files, currentDepth + 1, maxDepth); | |
} | |
} | |
catch (Exception ex) | |
{ | |
//Console.WriteLine($"Warning: Unable to access directory '{path}'. {ex.Message}"); | |
} | |
} | |
} | |
class FileStatistics | |
{ | |
private int totalExe = 0, validExe = 0; | |
private int totalDll = 0, validDll = 0; | |
public void IncrementTotal(string extension) | |
{ | |
if (extension == ".exe") | |
{ | |
totalExe++; | |
} | |
else if (extension == ".dll") | |
{ | |
totalDll++; | |
} | |
} | |
public void IncrementValid(string extension) | |
{ | |
if (extension == ".exe") | |
{ | |
validExe++; | |
} | |
else if (extension == ".dll") | |
{ | |
validDll++; | |
} | |
} | |
public void ReportStatistics() | |
{ | |
Console.WriteLine($"Total EXE files: {totalExe}, Valid .NET EXE files: {validExe}"); | |
Console.WriteLine($"Total DLL files: {totalDll}, Valid .NET DLL files: {validDll}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment