C#
  Home arrow C# arrow Page 3 - A Look at C# File and FileInfo Classes
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# File and FileInfo Classes
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2007-01-30

    Table of Contents:
  • A Look at C# File and FileInfo Classes
  • The File and FileInfo Classes
  • Moving the files to another directory
  • Copying the files to MyFolder3

  • 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# File and FileInfo Classes - Moving the files to another directory


    (Page 3 of 4 )

    The following code moves the files from MyFolder1 directory to MyFolder2. So let's take a look at it.

    using System;
    using System.IO;

    namespace IOProject
    {
      class Class1
      {
        static void Main(string[] args)
        {
          try
          {
            DirectoryInfo dirInfo = new DirectoryInfo
    (@"F:MyFolderMyFolder1");
            DirectoryInfo dirInfo2 = new DirectoryInfo
    (@"F:MyFolderMyFolder2");
            Console.WriteLine("Listing the files in MyFolder2:");
            FileInfo[] myFolder2Files = dirInfo2.GetFiles();
            if(myFolder2Files.Length > 0)
            {
              foreach(FileInfo aFile in myFolder2Files)
              {
                Console.WriteLine(aFile.FullName);
              }
            }
            else
            {
              Console.WriteLine("There are no files
    MyFolder2...n");
            }
            Console.WriteLine("Moving the files from MyFolder1 to
    MyFolder2");
            FileInfo[] myFolder1Files = dirInfo.GetFiles();
            foreach(FileInfo aFile in myFolder1Files)
            {
              if(File.Exists(@"F:MyFolderMyFolder2" + aFile.Name))
              {
                File.Delete(@"F:MyFolderMyFolder2" + aFile.Name);
              }
              aFile.MoveTo(@"F:MyFolderMyFolder2" + aFile.Name);
            }
            Console.WriteLine("The files have been moved to
    MyFolder2 directory");
            Console.WriteLine("Listing the files in MyFolder2:");
            myFolder2Files = dirInfo2.GetFiles();
            if(myFolder2Files.Length > 0)
            {
              foreach(FileInfo aFile in myFolder2Files)
              {
                Console.WriteLine(aFile.FullName);
              }
            }
            else
            {
              Console.WriteLine("There are no files in the directory
    MyFolder2");
            }
          }
          catch(IOException ex)
          {
            Console.WriteLine(ex.Message);
          }
          finally
          {
            Console.ReadLine();
          }
         }
      }
    }

    Run the above code and you will get the following screen shot. Don't forget to modify the drive letter.

    Now the files have been moved to the MyFolder2 directory.

    Let's walk through the code. The first two statements of the try block in the Main() method create two DirectoryInfo objects that correspond to the folders MyFolder1 and MyFolder2. The next thing we do is list the files in the MyFolder2 directory (which has no files, but you may have created some files there) by creating a FileInfo array and assigning it a return array value. We call the method dirInfo2.GetFiles(), then we use an if statement to check whether myFolder2Files.Length > 0 and if so, we list the files using a foreach statement. If there are no files, we write that to the Console.

    Now we need to move the files from MyFolder1 to MyFolder2. We do that using the FileInfo.MoveTo() instance method that accepts a destination path as a string value. Because we need to move more than one file we use the FileInfo.MoveTo() method inside a foreach statement.

    First, we get the files of the MyFolder1 directory using the dirInfo.GetFiles() method and assign it to a FileInfo array, then inside the foreach structure we check to see whether the file already exists in the MyFolder2 directory. If it exists, we delete it using the static method Delete() of the File class.

    You may want to create a FileInfo object and then call the Delete() method, but as we said before, this is just one operation that we are going to do on that file (the file that we want to delete in the MyFolder2 directory), so there is no need to create an object. It's sufficient to use the File.Delete() static method to delete the file.

    The method MoveTo() moves the file to MyFolder2 with the same file name. Note that you can specify a new name for the file with the MoveTo() method.

    We again use the GetFiles() method of the object dirInfo2 to get its files as an array of FileInfo objects. We then assign this array to myFolder2Files variable, and we use it in the foreach statement to list the files. Let's do one more thing.

    More C# Articles
    More By Michael Youssef


       · Please feel free to post your comment about this article.Michael
       · I would like to mention that when working on a web site you need to have in mind...
     

    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 6 hosted by Hostway
    Stay green...Green IT