C#
  Home arrow C# arrow Page 3 - A Look at C# Directory and DirectoryInfo C...
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

A Look at C# Directory and DirectoryInfo Classes
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2007-01-29

    Table of Contents:
  • A Look at C# Directory and DirectoryInfo Classes
  • Modifying the Example
  • Using the Directory Class
  • Deleting Directories

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    A Look at C# Directory and DirectoryInfo Classes - Using the Directory Class


    (Page 3 of 4 )

    As we have said, the Directory Class contains only static members and it's best used when you need to perform one or two operations on a specific directory. The idea is that there is no need to instantiate a DirectoryInfo class just to perform one operation on a directory; instead, use the static methods of the Directory Class to perform this operation. The following example uses the Directory Class to list its subdirectories.

    using System;
    using System.IO;

    namespace IOProject
    {
      class Class1
      {
        static void Main(string[] args)
        {
          try
          {
            string[] subDirs = Directory.GetDirectories(@"F:MyFolder");
            foreach(string subDir in subDirs)
            {
              Console.WriteLine("Folder Full Name: " + subDir.ToString());
            }
          }
          catch(Exception ex)
          {
            Console.WriteLine(ex.Message);
          }
          finally
          {
            Console.ReadLine();
          }
        }
      }
    }

    Copy and Paste the code in place of the Class1.cs code and press F5 to run the application. You will get the following screenshot:

    Because we just need to print out the name of the subdirectories of the directory named MyFolder we have used the Directory.GetDirectories() static method. This method accepts a path to a directory and returns its subdirectories as a string array. Bear in mind that the DirectoryInfo.GetDirectories() method returns an array of DirectoryInfo objects that we can manipulate (as we have done in the previous section). But the Directory.GetDirectories() method returns the subdirectories as an array of strings, and contains only the full name of the subdirectories.

    You should also note that the Directory.GetDirectories() accepts the name of the folder that you need to return its subdirectories, whereas the DirectoryInfo.GetDirectories() doesn't accept the folder path because it's working on a specific instance of a directory. Now let's modify the example to return DirectoryInfo objects for the subdirectories of the MyFolder directory.

    Passing the Path to Instantiate DirectoryInfo

    In the following code we create a DirectoryInfo object by passing the string that returns from the Directory.GetDirectories() method. Let's take a look at the code:

    using System;
    using System.IO;

    namespace IOProject
    {
      class Class1
      {
        static void Main(string[] args)
        {
          try
          {
            string[] subDirs = Directory.GetDirectories(@"F:MyFolder");
            foreach(string subDir in subDirs)
            {
              DirectoryInfo subDInfo = new DirectoryInfo(subDir);
              Console.WriteLine("Folder Full Name: {0}", subDInfo.FullName);
              Console.WriteLine("Creation Date : {0}", subDInfo.CreationTime);
              Console.WriteLine("Attributes: {0}", subDInfo.Attributes);
              Console.WriteLine();
            }
          }
          catch(Exception ex)
          {
            Console.WriteLine(ex.Message);
          }
          finally
          {
            Console.ReadLine();
          }
        }
      }
    }

    When you run the above code you will get the following screenshot

    The only trick in this example is that we have used the string array that returns from calling the Directory.GetDirectories() method to construct DirectoryInfo objects inside the foreach statement. The code simply prints out some of the properties of the DirectoryInfo objects like FullName, CreationTime and Attributes. The Directory Class has methods like GetCreationTime(), GetLastAccessTime(), and GetParent() that correspond to some of the properties of the DirectoryInfo class, but of course there are members that are unique to each class. For example, the Directory.GetCurrentDirectory() returns the directory of the application.

    More C# Articles
    More By Michael Youssef


       · Please feel free to post your comment about this article
     

    C# ARTICLES

    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway