CSharp.Net-Informations.com
   Home      .Net Framework      VB.NET      C#                                                                      About


  How to use C# Directory Class

Directory class in CSharp exposes methods to create , delete , move etc. operations to directories and subdirectories . Because of the static nature of C# Directory class , we do not have to instantiate the class. We can call the methods in the C# Directory class directly from the Directory class itself.

How to create a directory using Directory class in C# ?

In order to create a new directory using Directory class in C# , we can call CreateDirectory method directly from Directory class.

  Syntax : Directory.CreateDirectory(string DirPath)
  DirPath : The name of the new directory

CSharp Code : Directory.CreateDirectory("c:\\testDir1");

How to check a directory exist or not using Directory class in C# ?

Before we creating a directory or folder , we usually check that directory or folder exist or not. In C# we are using Exists method in the Directory class.

  Syntax : bool Directory.Exist(string DirPath)
  DirPath : The name of the directory
  bool : Returns true or false -
  if directory exist it Returns true , else it Returns false

CSharp Code : Directory.Exists("c:\\testDir1")

How to move a Directory using Directory class in C# ?

If we want to move a directory and its contents from one location to another , we can use the Move method in the C# Directory class.

  Syntax : void Directory.Move(string sourceDirName,string destDirName)
  sourceDirName : The source directory we want to move.
  destDirName : The destinations directory name.

CSharp Code : Directory.Move("c:\\testDir1\\testDir2", "c:\\testDir");

How to delete a Directory using Directory class in C# ?

When we want to delete a directory we can use the Delete method in the C# Directory class

  Syntax : void Directory.Delete(string DirPath)
  DirPath : The Directory we want to delete.

CSharp Code : Directory.Delete("c:\\testDir1");

The following C# source code shows some operations in Directory class

         C# Source Code Download           Print Source Code
         How to use C# Directory Class - Download
        
C# Tutorial

using System;
using System.Windows.Forms;
using System.IO;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Directory.Exists("c:\\testDir1"))
            {
                //shows message if testdir1 exist 
                MessageBox.Show ("Directory 'testDir' Exist ");
            }
            else
            {
                //create the directory testDir1 
                Directory.CreateDirectory("c:\\testDir1");
                MessageBox.Show("testDir1 created ! ");
                //create the directory testDir2 
                Directory.CreateDirectory("c:\\testDir1\\testDir2");
                MessageBox.Show("testDir2 created ! ");
                //move the directory testDir2 as testDir in c:\ 
                Directory.Move("c:\\testDir1\\testDir2", "c:\\testDir");
                MessageBox.Show("testDir2 moved ");
                //delete the directory testDir1 
                Directory.Delete("c:\\testDir1");
                MessageBox.Show("testDir1 deleted ");
            }
        }
    }
}



CSharp File (I/O) Related Contents
*     How to use C# File Class
*     How to use C# FileStream Class
*     How to use C# Textreader Class
*     A simple C# Text Reader source code
*     How to use C# TextWriter Class
*     How to use C# BinaryWriter Class
*     How to use C# BinaryReader Class


   Home      VB.NET      C#
CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
*     C# DataGridView Tutorial
   Home      VB.NET      C#
More Source Code :   
Mail to :  feedback@net-informations.com
  |  Home   |  VB.NET   |  C#   |  SiteMap   |  Terms of Use   |  About   |
net-informations.com (C) 2010
All Rights Reserved. All other trademarks are property of their respective owners.