Monday 26 May 2014

how to Create And Delete Directory or Folder in ASP.NET Using C#




Description:-



In this example I will explain how to create and delete folder or directory in ASP.Net using C# . On many sitution we have requirement to create and delete folder or directory in ASP.Net web Aplication.

To do this you have to use system.io namespace for createing and deleting facility of the directory or file. i this application  First the path is generated using the folder (directory) Name from the TextBox.

While creating a folder or directory at that time we also provide facility to user that is, first it checks whether the folder or directory with the same name exists if no then the folder is created otherwise user gets a  alert message that the folder with the name already exists.

Same facility provide in deleting  facility While deleting a folder, first it checks whether the folder ( with the same name exists if yes then the folder  is deleted else user gets a JavaScript alert notification that the folder  with the name does not exists and hence cannot be deleted.



sorting data in gridview with up and down arrow sorting data with up and down arrow in gridview

Ajax Autocomplete Textbox search example autocomplete textbox search in asp.net



Cs.aspx:-


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Create or Delete Directory in Asp.Net with C#</title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    Enter Directory or Folder Name:
    <asp:TextBox ID="txtdororfolName" runat="server" Text="Test"></asp:TextBox>
    <br />
    <asp:Button ID="btnfolderCreate" runat="server" Text="Create Folder" OnClick="btnfolderCreate_Click" />
    <asp:Button ID="btnfolderDelete" runat="server" Text="Delete Folder" OnClick="btnfolderDelete_Click" />
    </form>
</body>
</html>


Cs.aspx.cs:-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class CS : System.Web.UI.Page
{
    protected void btnfolderCreate_Click(object sender, EventArgs e)
    {
        string directoryPath = Server.MapPath(string.Format("~/{0}/", txtdororfolName.Text.Trim()));
        if (!Directory.Exists(directoryPath))
        {
            Directory.CreateDirectory(directoryPath);
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Directory created successfully.');", true);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Directory already exists.');", true);
        }
    }

    protected void btnfolderDelete_Click(object sender, EventArgs e)
    {
        string directoryPath = Server.MapPath(string.Format("~/{0}/", txtdororfolName.Text.Trim()));
        if (Directory.Exists(directoryPath))
        {
            Directory.Delete(directoryPath);
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Directory Deleted successfully.');", true);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Directory does not exists.');", true);
        }
    }
}

0 comments:

Post a Comment