Cet exemple de code vous montre comment analyser le contenu d'un dossier via la librairie .NET et son namespace System.IO
.
Comme vous allez le constater, l'algorithme proposé est récursif et une analyse des sous-dossiers sera effectuée.
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 59 60 61 62 63 64 |
using System; using System.IO; namespace Listing { class Program { static void ScanPath(string path, string level = "") { var metadata = File.GetAttributes(path); bool isDir = (metadata & FileAttributes.Directory) != 0; if (isDir) { var directoryInfo = new DirectoryInfo(path); Console.WriteLine( "DIR {0,10} {1,15} {2} {3}", "", directoryInfo.LastWriteTime.ToLocalTime(), level, directoryInfo.Name ); foreach(var entry in directoryInfo.GetDirectories()) { ScanPath(entry.FullName, level + " "); } foreach (var entry in directoryInfo.GetFiles()) { ScanPath(entry.FullName, level + " "); } } else { var fileInfo = new FileInfo(path); Console.WriteLine( "FILE {0,10} ko {1,15} {2} {3}", fileInfo.Length / 1024, fileInfo.LastWriteTime.ToLocalTime(), level, fileInfo.Name ); } } static void Main(string[] args) { string[] paths = { "." }; if (args.Length != 0) paths = args; foreach(string path in paths) { if (! File.Exists(path) && ! Directory.Exists(path)) { Console.Error.WriteLine($"Le dossier {path} n'existe pas"); continue; } Console.WriteLine($"--- Listing for {path} ---"); ScanPath(path); Console.WriteLine("\n"); } } } } |
Et voici un exemple d'exécution sur le dossier racine du projet Visual Studio.
$> Listing.exe ..\..\.. --- Listing for ..\..\.. --- DIR 07/03/2021 21:24:05 Listing DIR 07/03/2021 20:55:59 obj DIR 07/03/2021 20:55:46 Debug DIR 07/03/2021 21:24:11 netcoreapp3.1 FILE 1 ko 07/03/2021 21:18:17 Listing.csproj.FileListAbsolute.txt FILE 1 ko 07/03/2021 20:55:47 Listing.AssemblyInfo.cs FILE 0 ko 07/03/2021 20:56:01 Listing.assets.cache FILE 170 ko 07/03/2021 21:24:12 apphost.exe FILE 0 ko 07/03/2021 21:18:51 Listing.csprojAssemblyReference.cache FILE 5 ko 07/03/2021 21:24:11 Listing.dll FILE 0 ko 07/03/2021 20:55:47 Listing.AssemblyInfoInputs.cache FILE 0 ko 07/03/2021 20:55:47 .NETCoreApp,Version=v3.1.AssemblyAttributes.cs FILE 9 ko 07/03/2021 21:24:11 Listing.pdb FILE 0 ko 07/03/2021 21:18:09 Listing.csproj.CoreCompileInputs.cache FILE 0 ko 07/03/2021 21:18:16 Listing.genruntimeconfig.cache FILE 1 ko 07/03/2021 20:55:59 Listing.csproj.nuget.g.props FILE 0 ko 07/03/2021 20:55:59 Listing.csproj.nuget.g.targets FILE 0 ko 07/03/2021 20:55:59 project.nuget.cache FILE 2 ko 07/03/2021 20:55:59 Listing.csproj.nuget.dgspec.json FILE 2 ko 07/03/2021 20:55:59 project.assets.json DIR 07/03/2021 20:55:46 bin DIR 07/03/2021 20:55:46 Debug DIR 07/03/2021 21:18:17 netcoreapp3.1 FILE 0 ko 07/03/2021 21:18:16 Listing.deps.json FILE 5 ko 07/03/2021 21:24:11 Listing.dll FILE 9 ko 07/03/2021 21:24:11 Listing.pdb FILE 170 ko 07/03/2021 21:24:12 Listing.exe FILE 0 ko 07/03/2021 21:18:17 Listing.runtimeconfig.json FILE 0 ko 07/03/2021 21:18:17 Listing.runtimeconfig.dev.json FILE 1 ko 07/03/2021 21:24:05 Program.cs FILE 0 ko 07/03/2021 20:55:22 Listing.csproj $>
Améliorations / Corrections
Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.
Emplacement :
Description des améliorations :