C#
  Home arrow C# arrow Page 4 - 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 - Copying the files to MyFolder3


    (Page 4 of 4 )

    For this example we need to write and save the following text into the files of the MyFolder2 directory using Notepad:

    The files contains some text

    Create a file named File1.txt in the MyFolder3 directory but leave it empty. Now let's move to the code.

    using System;
    using System.IO;

    namespace IOProject
    {
      class Class1
      {
        static void Main(string[] args)
        {
          try
          {
            DirectoryInfo dirInfo2 = new DirectoryInfo
    (@"F:MyFolderMyFolder2");
            DirectoryInfo dirInfo3 = new DirectoryInfo
    (@"F:MyFolderMyFolder3");
            Console.WriteLine("Listing the files in MyFolder2:");
    FileInfo[] myFolder2Files = dirInfo2.GetFiles();
            if(myFolder2Files.Length > 0)
            {
              foreach(FileInfo aFile in myFolder2Files)
              {
                Console.WriteLine("File Name: {0}, Size:
    {1}",aFile.FullName, aFile.Length);
              }
              Console.WriteLine("n");
            }
            else
            {
              Console.WriteLine("There are no files
    MyFolder2...n");
            }

            Console.WriteLine("Listing the files in MyFolder3:");
            FileInfo[] myFolder3Files = dirInfo3.GetFiles();
            if(myFolder3Files.Length > 0)
            {
              foreach(FileInfo aFile in myFolder3Files)
              {
                Console.WriteLine("File Name: {0}, Size:
    {1}",aFile.FullName,aFile.Length);
              }
              Console.WriteLine("n");
            }
            else
            {
              Console.WriteLine("There are no files
    MyFolder3...n");
            }
            Console.WriteLine("Copying the files from MyFolder2 to
    MyFolder3");

            foreach(FileInfo aFile in myFolder2Files)
            { 
              aFile.CopyTo(@"F:MyFolderMyFolder3" + aFile.Name,
    true);
            }
            Console.WriteLine("The files have been copied to
    MyFolder3 directory"); 
            Console.WriteLine("Listing the files in MyFolder3:");
            myFolder3Files = dirInfo3.GetFiles();
            if(myFolder3Files.Length > 0)
            {
              foreach(FileInfo aFile in myFolder3Files)
              {
                Console.WriteLine("File Name: {0}, Size:
    {1}",aFile.FullName,aFile.Length);
              }
            }
            else
            {
            Console.WriteLine("There are no files in the directory
    MyFolder3");
            }
          }
          catch(IOException ex)
          {
            Console.WriteLine(ex.Message);
          }
          finally
          {
            Console.ReadLine();
          }
        }
      }
    }

    As usual, replace the above code with the code of the Class1.cs then press F5 to run the application.

    Here are the files in MyFolder3

    The code has nothing new except the CopyTo() method, which accepts a destination path and a Boolean value that indicates whether the file overwrites an existing file with the same name or not.

    Notice that we have listed the files of MyFolder2. We have found that all the files has the same size (28 Bytes) because they contain the same text. When we have listed the files in MyFolder3, we found the only file that we created (the empty file1.txt file) has a size of 0 Bytes. After the copying operation we found that the file MyFolder3file1.txt has the same size as the other files (28 Bytes) because it has been overwritten by the CopyTo() method.

    In my next article I'm going to show you how to write to and read from files using stream-based classes.


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