C#
  Home arrow C# arrow Page 3 - Managing Files in C#
Iron Speed
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 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 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#

Managing Files in C#
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2008-03-11

    Table of Contents:
  • Managing Files in C#
  • The Foundations
  • Copying, Moving, and Deleting
  • Renaming and File Attributes
  • Final Words

  • 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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Managing Files in C# - Copying, Moving, and Deleting
    (Page 3 of 5 )

    Due to the nature of our project, we need to have a source and a destination file. That's what we are going to use on almost every action during our tutorial - copy, move, etc. The destination won't be use as frequently as the source, but we still need to have it prepared. To accomplish this, we are going to create two strings, sourceFileName and destFileName. After this, we can code the dialog boxes' [OK].


    private void openFileDialog_FileOk(object sender, CancelEventArgs e)

    {

    sourceFileName = openFileDialog.FileName;

    }

    private void saveFileDialog_FileOk(object sender, CancelEventArgs e)

    {

    destFileName = saveFileDialog.FileName;

    }


    You can guess what the above block of code does, right? It's intuitive. As soon as the user picks his/her source and destination files in the dialog boxes, those two variables will carry their address/path (i.e. C:Tempdatabase.txt).

    Let the fun begin! Now we will code what happens when we click on btnCopy. The snippet is attached below, but first let me explain it in a nutshell. We display/run the dialog boxes because we need the source and destination paths. A quick bit of error handling is necessary because if there is a "null" or empty path, our copy method goes nuts. As soon as this is done we use System.IO.File.Copy() to copy the file. Then we notify the user with a pretty `Done with success` message box.


    private void btnCopy_Click(object sender, EventArgs e)

    {

    sourceFileName = null;

    destFileName = null;

    openFileDialog.ShowDialog();

    if (!System.IO.File.Exists(sourceFileName))

    {

    MessageBox.Show("The selected file either does not exist or you didn't pick any.");

    return;

    }

    saveFileDialog.ShowDialog();

    if (destFileName == null || destFileName == "")

    {

    MessageBox.Show("You didn't pick the destination path.");

    return;

    }

    System.IO.File.Copy(sourceFileName, destFileName);

    MessageBox.Show("Done with success.");

    }


    We do the same for btnMove, too. The only difference is that we will use the System.IO.File.Move() to accomplish the file-moving action. The rest remains the same.


    private void btnMove_Click(object sender, EventArgs e)

    {

    sourceFileName = null;

    destFileName = null;

    openFileDialog.ShowDialog();

    if (!System.IO.File.Exists(sourceFileName))

    {

    MessageBox.Show("The selected file either does not exist or you didn't pick any.");

    return;

    }

    saveFileDialog.ShowDialog();

    if (destFileName == null || destFileName == "")

    {

    MessageBox.Show("You didn't pick the destination path.");

    return;

    }

    System.IO.File.Move(sourceFileName, destFileName);

    MessageBox.Show("Done with success.");

    }


    Next is btnDelete, because it's the easiest. We will skip renaming and grabbing the file attributes for now. You will find those on the next page. Delete works based on a sourceFileName, so requiring the user to specify a destination file is silly! We will do the usual error handling and then, for convenience, we'll display a confirmation message box. The user might miss-click or your code might be buggy and then something important becomes deleted. After clicking "Yes," we delete the file.


    private void btnDelete_Click(object sender, EventArgs e)

    {

    sourceFileName = null;

    openFileDialog.FileName = null;

    openFileDialog.ShowDialog();

    if (!System.IO.File.Exists(sourceFileName))

    {

    MessageBox.Show("The selected file either does not exist or you didn't pick any.");

    return;

    }

    if (MessageBox.Show("Are you sure? You are about to delete the following file: " + sourceFileName, "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)

    {

    System.IO.File.Delete(sourceFileName);

    MessageBox.Show("Done with success.");

    }

    }


    The rename and properties parts are a bit longer, but that doesn't necessarily mean they're more complex or advanced. We'll do our best to explain what each snippet does and why we opted for that type of algorithm, have no fear. Turn the page, if you're ready.

    More C# Articles
    More By Barzan "Tony" Antal


       · Thanks for tuning in to read this tutorial. I hope you've found it educational and...
     

    C# ARTICLES

    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained
    - A Look at C# File and FileInfo Classes
    - A Look at C# Directory and DirectoryInfo Cla...




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