C#
  Home arrow C# arrow Page 5 - C# Simplified, part 5: Error Handling and ...
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#

C# Simplified, part 5: Error Handling and Files
By: Anand Narayanaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 15
    2005-05-31

    Table of Contents:
  • C# Simplified, part 5: Error Handling and Files
  • Try-Catch clause
  • Finally clause
  • Accessing files and directories
  • Displaying all files under a directory
  • Creating files
  • Reading from a file
  • Copying a file

  • 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


    C# Simplified, part 5: Error Handling and Files - Displaying all files under a directory


    (Page 5 of 8 )

    It is very easy to populate all files within a directory by using C#. All you need to do is create an instance of DirectoryInfo class and call the GetFiles() method. Since you are displaying all the files, you must use the foreach() loop. Listing 5.6 shows you how to display all jpeg files under a particular directory.

    Listing 5.6

    DirectoryInfo dir = new DirectoryInfo(@”F:\WINNT”);

    FileInfo[] bmpfiles = dir.GetFiles(“*.bmp);

    Console.WriteLine(“Total number of bmp files” , bmpfiles.Length);

    Foreach( FileInfo f in bmpfiles)

    {

    Console.WriteLine(“Name is : {0}”, f.Name);

    Console.WriteLine(“Length of the file is : {0}”, f.Length);

    Console.WriteLine(“Creation time is : {0}”, f.CreationTime);

    Console.WriteLine(“Attributes of the file are : {0}”, f.Attributes.ToString());

    }

    Creating Subdirectories

    C# enables you to programmatically create a subdirectory with the help of the CreateSubDirectory() method. In listing 5.7, a subdirectory named MYSUB is created under SUB directory. The new directory will be created on the D drive of your system.

     

    Listing 5.7

    using System;

    using System.IO;

          class CreateDir

          {

                public static void Main()

                {

                     

                      DirectoryInfo dir = new DirectoryInfo(@"D:\");

                      try

                      {

                         dir.CreateSubdirectory("SUB");

                         dir.CreateSubdirectory(@"SUB\MYSUB");

                      }

                      catch(IOException e)

                      {

                         Console.WriteLine(e.Message);

                      }

                }

          }

    After executing the above code, go to the D drive and verify the existence of the newly created directories.

    More C# Articles
    More By Anand Narayanaswamy


     

    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