Wednesday, May 25, 2011

Create, Delete & Move Directory Using C#.Net?

Hi friend here I have written code for Creating, Delete and Move dirrctory.

In My createDirectory.aspx page I have use one text box and three button.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class CreateDirectory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        string FolderName = txtFolder.Text;
        string Path = Server.MapPath(FolderName);

        try
        {
            // Check if directory exists
            if (!Directory.Exists(Path))
            {
                // Create the directory.
                Directory.CreateDirectory(Path);
            }

        }
        catch (IOException _ex)
        {
            Response.Write(_ex.Message);
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string FolderName = txtFolder.Text;
        string Path = Server.MapPath(FolderName);

        try
        {
            // Check if directory exists
            if (Directory.Exists(Path))
            {
                // Delete the directory.
                Directory.Delete(Path);
            }

        }
        catch (IOException _ex)
        {
            Response.Write(_ex.Message);
        }
    }
    protected void btnMove_Click(object sender, EventArgs e)
    {
        string FolderName = txtFolder.Text;
        string sPath = Server.MapPath(FolderName);
        string dPath = Server.MapPath("MoveItem\\" + FolderName);

        try
        {
            // Check if directory exists
            if (Directory.Exists(sPath))
            {
                // Move the directory.
                Directory.Move(sPath, dPath);
               
            }

        }
        catch (IOException _ex)
        {
            Response.Write(_ex.Message);
        }
    }
}

Before deleting directory make sure directory is empty.

No comments:

Post a Comment