C#
  Home arrow C# arrow Page 4 - 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 
Mobile Linux 
App Generation ROI 
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 / 13
    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


    A Look at C# Directory and DirectoryInfo Classes - Deleting Directories


    (Page 4 of 4 )

    We want to delete some subdirectories in our MyFolder directory. The following code example deletes MyFolder2 and MyFolder4 using the Directory.Delete() method, which accepts a path to the folder that we want it to delete.

    using System;
    using System.IO;

    namespace IOProject
    {
      class Class1
      {
        static void Main(string[] args)
        {
          try
          {
            string[] subDirs = Directory.GetDirectories(@"F:MyFolder");
            foreach(string temp in subDirs)
            {
              int dirNum = Convert.ToInt32(temp.Substring(temp.Length - 1,1));
              if(dirNum % 2 == 0)
              {
                Directory.Delete(temp);
                Console.WriteLine("the Folder {0} has been deleted.", temp);
              }
            }
          }
          catch(Exception ex)
          {
            Console.WriteLine(ex.Message);
          }
          finally
          {
            Console.ReadLine();
          }
        }
      }
    }

    There is nothing special about this code except that we have used the String.SubString() method to return the last character in the directory name. That character is a number, so we have converted it from a string data type to an int data type. The if statement evaluates to true if the returned value of the expression dirNum % 2 is 0. So only the folders MyFolder2 and MyFolder4 are deleted because 2 % 2 == 0 and 4 % 2 = 0.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Please feel free to post your comment about this article
     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT