C#
  Home arrow C# arrow Page 2 - A Look at C# Directory and DirectoryInfo C...
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 
Moblin 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    A Look at C# Directory and DirectoryInfo Classes - Modifying the Example


    (Page 2 of 4 )

    Copy the following code and paste it in to replace the previous code example:

    using System;
    using System.IO;

    namespace IOProject
    {
      class Class1
      {
        static void Main(string[] args)
        {
          try
          {
            Console.WriteLine("Please enter the name of the folder you want to create or use");
            // store the folder name in the dirName variable
            string dirName = Console.ReadLine();
            // create the DirectoryInfo instance
            DirectoryInfo dInfo = new DirectoryInfo(@"F:" + dirName);
            // check if the directory doesn't exist, if so then
            // create it
            if(!dInfo.Exists)
            {
              dInfo.Create();
            }

            Console.WriteLine("How many subfolders you want to create?");
            // store the number of subfolders in the variable
            // subDirs
            int subDirs = Convert.ToInt32(Console.ReadLine());
            // create the subfolders using a for statement
            for(int i = 1; i <= subDirs; i++)
            {
              dInfo.CreateSubdirectory(dirName + i);
            }
            Console.WriteLine("The following Subfolders have been" +
    " created in the folder " + dirName);
            // get the subfolders using the
            // DirectoryInfo.GetDirectories() method
            DirectoryInfo[] dirs = dInfo.GetDirectories();
            // printing the first subfolder information
            DirectoryInfo firstSDir = dirs[0];
            Console.WriteLine(firstSDir.FullName);
            Console.WriteLine(firstSDir.Name);
            Console.WriteLine(firstSDir.Root);
            Console.WriteLine(firstSDir.CreationTime);
            Console.WriteLine(firstSDir.LastAccessTime);
            Console.WriteLine(firstSDir.Attributes);
            firstSDir.Attributes |= FileAttributes.Archive;
            Console.WriteLine(firstSDir.Attributes);
            Console.WriteLine("The Folder Extension : " + firstSDir.Extension);
          }
          catch(Exception ex)
          {
            Console.WriteLine(ex.Message);
          }
          finally
          {
            Console.ReadLine();
          }
        }
      }
    }

    Run the code and enter the same values as the previous example.

    Because the MyFolder directory already exists, the if statement evaluates to false and the next statement (after the if statement) is executed. We entered the value 4 as before, so the DirectoryInfo.CreateSubDirectories will not create any subfolders (because the subfolders are already there). The difference is that we have replaced the foreach statement with the following code:

    DirectoryInfo firstSDir = dirs[0];
    Console.WriteLine(firstSDir.FullName);
    Console.WriteLine(firstSDir.Name);
    Console.WriteLine(firstSDir.Root);
    Console.WriteLine(firstSDir.CreationTime);
    Console.WriteLine(firstSDir.LastAccessTime);
    Console.WriteLine(firstSDir.Attributes);
    firstSDir.Attributes |= FileAttributes.Archive;
    Console.WriteLine(firstSDir.Attributes);
    Console.WriteLine("The Folder Extension : " + firstSDir.Extension);

    The above code prints some useful information about the first subfolder in the MyFolder directory. This information includes the Name property, which returns the name of the folder; the Root property which returns the root; and the CreationTime and lastAccessTime properties are self explanatory.

    The Attribute property is used to set or get some useful attributes of the file. Note that you use the | operator to combine those attributes. When we first print the value of the Attributes property we find that there is only one attribute set (Directory). We add another attribute using the FileAttributes Enumeration and print the value of Attributes property again. The Extension property returns an empty string when called on DirectoryInfo instances. The Extension property is inherited, as are much of the methods and properties of this class, from the base class FileSystemInfo. To check that the Extension property returns an empty string you can use the following code:

    DirectoryInfo di = new DirectoryInfo(@"F:MyFolder");
    if(di.Extension == String.Empty)
    {
      Console.WriteLine("Yes Empty String");
    }

    More C# Articles
    More By Michael Youssef


       · Please feel free to post your comment about this article
     

    C# ARTICLES

    - Color Transformation in C# GDI+ Programming
    - 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





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