C#
  Home arrow C# arrow 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 11
    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


    (Page 1 of 4 )

    When you think about creating files programmatically in C# you have the option of using the File class or the FileInfo class. In this article, we are going to create, copy and move files using those classes. This is the second article in a multi-part series.

    You need to read my earlier article, "A Look at C# Directory and DirectoryInfo Classes," before you read this one because we are going to use the concepts explained in that article here. You need VS.NET 2003 installed on your machine in order to run the code examples. Before you run the examples, you may need to change the drive letter because I'm using the drive letter F (actually, this is an empty drive on my machine).

    We discuss the Path class first. Let's get to work.

    Understanding the Path Class

    As you already know, a path is a string that tells you where your file or directory is located. So F:MyFolderMyFolder1help.txt is a path to the file help.txt which tells us that the file exists in the folder MyFolder1, which exists in the MyFolder directory. Different platforms use different separator characters, but we can use the Path class to build path names without worrying about this issue.

    The Path class contains only static members such as the Path.Combine() method, which you use to build the path name that's platform-specific. The Path class provides us with other useful members that are illustrated by the following example.

    using System;
    using System.IO;

    namespace IOProject
    {
      class Class1
      {
        static void Main(string[] args)
        {
          Console.WriteLine("The Path Separator Character = {0}",
          Path.DirectorySeparatorChar);
          Console.WriteLine("The Path Alternative Separator
    Character = {0}",
          Path.AltDirectorySeparatorChar);
          Console.WriteLine("The Path Separator = {0}",
    Path.PathSeparator);
          Console.WriteLine("Combining the string " F:MyFolder "
    and " + "" MyFolder1 " using Path.Combine()");
          Console.WriteLine(Path.Combine("F:MyFolder",
    "MyFolder1"));
          Console.ReadLine();
        }
      }
    }

    To run the above code create a new VS.NET Console Application Project, replace the Class1.cs auto-generated code with the above code, then run it.

    The first line in the Main() method prints out the value of the Property Path.DirectorySeparatorChar, the "" character, which is used on the Windows operating system to separate the names of folders and files to build the path; the value of this Property on Unix is "/". The next line prints out the value of the Property Path.AltDirectorySeparatorChar which is the "/" character on Windows and "" in the Unix Operating System to separate the names of folders and files  to build the path. The Path.PathSeparator property returns the path separator character ;. The Path.Combine() method combines two strings and forms one path name. So the method call Path.Combine("F:MyFolder", "MyFolder1") returns F:MyFolderMyFolder1. There are some other members of the Path class but we will not discuss them. Instead, we are going to begin our discussion of the FileInfo and File Classes.

    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

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek