Hallo,
ich habe ein Programm geschrieben, das aus einem gegeben Ordner alle Dateien und Unterordner in eine Datei auflistet. Zudem wird zu jeder Datei ein SHA512-Code generiert.
Hier erst einmal der Quellcode:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58:
| public static void CreateFileList(string pathFileList, string pathMainDirectory) {
if(progress == 1.0) {
progress = 0.0;
Thread thread = new Thread(delegate() {
double progressStep = 1.0 / Directory.EnumerateFiles(pathMainDirectory, "*", SearchOption.AllDirectories).Count(); File.WriteAllText(pathFileList, string.Empty); Action<string> writeFileList = null;
writeFileList = delegate(string pathCurrentDirectory) {
IEnumerable<string> pathsSubdirectories = Directory.EnumerateDirectories(pathCurrentDirectory); List<string> data = new List<string>(); data.AddRange(from pathCurrentSubdirectory in pathsSubdirectories select Path.GetFileName(pathCurrentSubdirectory));
foreach(FileInfo currentFile in (new DirectoryInfo(pathCurrentDirectory)).EnumerateFiles()) {
if(!currentFile.Name.Equals(Path.GetFileName(pathFileList))) {
data.Add(currentFile.Name + Path.DirectorySeparatorChar.ToString() + FileGetHashCode(currentFile.FullName)); progress += progressStep;
}
}
File.AppendAllLines(pathFileList, data);
foreach(string pathCurrentSubdirectory in pathsSubdirectories) {
File.AppendAllText(pathFileList, Environment.NewLine + pathCurrentSubdirectory.Replace(pathMainDirectory, string.Empty) + Environment.NewLine); writeFileList(pathCurrentSubdirectory);
}
};
writeFileList(pathMainDirectory);
if(progress != 1.0) progress = 1.0;
});
thread.IsBackground = true; thread.Start();
} else {
throw new InvalidOperationException("Program is still performing another operation");
}
} |
Die Methode befindet sich in einer Klasse mit der Eigenschaft
progress (als double von 0 bis 1; dieser wird mit 100 multipliziert und als Integer zurückgegeben), mit der der aktuelle Fortschritt dargestellt wird.
Ich habe zum ersten mal mit Delegates gearbeitet, was haltet ihr davon? Ich lese die Unterordner mit Rekursion aus und das in einem Thread, deshalb die Delegates.
Könnt ihr mir Verbesserungsvorschläge geben oder ist das so in Ordnung, Danke!