EziData Solutions

Web, NET and SQL Server

C# Getting Information about your Folders

Continuing on our journey converting old VBA code to C#, in this post we’ll revisit getting information about the folders/directories on a system. In .NET parlance, folders on the file system are directories, so from here on in, that’s what we’ll call them.

In the last post we looked at how to retrieve a list of drives on the system, so in this post we’ll create a Console Application to return a list of directories in a specific drive.

Ensure you have added the System.IO namespace to Program.cs.

Code Snippet
  1. using System.IO;

Copy the following code into the Main method. This code will iterate through the directories under C-drive and print the directory name in the console. NOTE: any slashes '\' in the directory path need to be escaped - for exampl, we have C:\\, not C:\.

Code Snippet
  1. // set the root or starting directory
  2. DirectoryInfo dir = new DirectoryInfo("C:\\");
  3.  
  4. // get all the children under the root
  5. DirectoryInfo[] children = dir.GetDirectories();
  6.  
  7. foreach (DirectoryInfo d in children)
  8. {
  9.     Console.WriteLine(d.Name);
  10. }

In the code above we set the starting directory to the C-drive root, but it is important to know that you can set the starting directory to any valid directory on the system, not just drives.

It is possible to continue down the system hierarchy and return the children of each of directory by calling the GetDirectories method on each. Writing a function that recursively calls GetDirectories for each subsequent DirectoryInfo object would effectively traverse the entire directory structure.

Posted: Jul 14 2010, 10:22 by CameronM | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: C#

C# Getting Information about your Drives

Many moons ago I wrote a series of posts on how to access the file system from your Access database using Scripting.FileSystemObject in VBA. Just for fun and partly as a learning experience, I thought it was time to look at how the same can be achieved in .NET and more specifically C#.

This first post I wrote covered how to get information about the drives currently available on the host system. Here’s the VBA to .NET comparison.

VBA: Scripting.FileSystemObject.Drives = .NET: System.IO.DriveInfo.GetDrives()

To get started, open Visual Studio 2010 and create a new Console Application. In the Program.cs file add a using statement for the System.IO namespace, which contains all of the methods you’ll need to access drives, directories and files.

Code Snippet
  1. using System.IO;

Now insert the following code in the Main method. This code will get an array of DriveInfo objects, which contain the basic information of all the drives available on your system.

Code Snippet
  1. // get a list of drives available on the system
  2. DriveInfo[] drives = DriveInfo.GetDrives();
  3.  
  4. foreach (DriveInfo drive in drives)
  5. {
  6.     Console.WriteLine(drive.Name);
  7. }

Some interesting properties of the DriveInfo object are Name, AvailableFreeSpace and DriveForm (FAT/NTFC).

Posted: Jul 11 2010, 17:56 by CameronM | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: C#