TOC

This article is currently in the process of being translated into Dutch (~38% done).

Bestandsbehandeling:

Informatie over Bestand en directory

De File en de Directory classes die we in de vorige paar hoofdstukken hebben gebruikt zijn uitstekend geschikt voor directe bestands- en directory manipulatie. Maar soms ook willen we informatie hebben óver bestand en directory, en alweer komt de System.IO namespace ont te hulp: De FileInfo en DirectoryInfo classes. In dit hoofdstuk kijken we naar een paar manier om ze te gebruiken.

De FileInfo class

Laten we eerst even naar een simpele manier kijken om de FileInfo class te gebruiken.

static void Main(string[] args)
{
    FileInfo fi = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
    if(fi != null)
Console.WriteLine(String.Format("Information about file: {0}, {1} bytes, last modified on {2} - Full path: {3}", fi.Name, fi.Length, fi.LastWriteTime, fi.FullName));
    Console.ReadKey();
}

We maken een nieuw exemplaar van de FileInfo class. Hij krijgt één parameter, dat het pad is naar het bestand waar we iets van willen weten. We hadden natuurlijk ook alleen de naam van het bestand kunnen aangeven, maar ik dacht dat het leuker was om info te krijgen over de applicatie waar we aan werken, en dat is het EXE bestand waarin ons bestand is gecompileerd. Omdat we in een Console applicatie geen toegang hebben tot het Applicatie project ( Console is deel van de Winforms assemblage), gebruiken we wat Reflection om het pad naar de assembly te krijgen. Dit is volslagen buiten de scope van dit speciale hoofdstuk, maar nu weet je er tenminste van.

Once we have a FileInfo instance, we output all sorts of information about it. Try running the application and you will see. All very nice and easy, and if you look at the FileInfo class, you will see that it offers even more information, as well as shortcuts to the methods found on the File class - and why not? We have a reference to the file anyway with the FileInfo instance, so we might as well get the same options as on the File class.

The DirectoryInfo class

Now, information about a single file is just fine, but using the DirectoryInfo class, we can get information about all files and directories within a directory, which is obviously a very common scenario. Let me show you with a simple example:

DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
if(di != null)
{
    FileInfo[] subFiles = di.GetFiles();
    if(subFiles.Length > 0)
    {
Console.WriteLine("Files:");
foreach(FileInfo subFile in subFiles)
{
    Console.WriteLine("   " + subFile.Name + " (" + subFile.Length + " bytes)");
}
    }
    Console.ReadKey();
}

Instead of a FileInfo instance, we create a DirectoryInfo instance. We use the same trick to get the path of the executing file, and then the GetDirectoryName() method from the Path class, to get the directory part of the path only. We use the GetFiles() method to get an array of FileInfo instances, each representing a file in the directory. We then loop through it, printing out each filename and size.

Perhaps we want the directories as well. It's just as easy:

DirectoryInfo[] subDirs = di.GetDirectories();
if(subDirs.Length > 0)
{
    Console.WriteLine("Directories:");
    foreach(DirectoryInfo subDir in subDirs)
    {
Console.WriteLine("   " + subDir.Name);
    }
}

In some situations, you might only want files or directories with a specific name or file extension. Fortunately, FileInfo and DirectoryInfo has some pretty good support for that as well.

This will give us all files in the directory with a .exe extension:

FileInfo[] subFiles = di.GetFiles("*.exe");

This will give us all the directories which have the word "test" somewhere in the name:

DirectoryInfo[] subDirs = di.GetDirectories("*test*");

We can even find both files and directories recursively, which means that it will search in subdirectories of subdirectories of.... the originial directory:

FileInfo[] subFiles = di.GetFiles("*.exe", SearchOption.AllDirectories);

To only search the toplevel directory, the code would have to look like this:

FileInfo[] subFiles = di.GetFiles("*.exe", SearchOption.TopDirectoryOnly);

Summary

Using the FileInfo and DirectoryInfo classes, we can easily discover information about the file system on the current computer.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!