asp.net - How to determine whether a directory exists

Determine whether a directory exists
.Net framework Directory.Exists() method allow us to determine whether the given path refers to an existing directory on disk. Directory class Exists() method is in System.IO namespace.

Directory.Exists() method has a required parameter named 'path'. the 'path' parameter value data type is System.String. this string value represents the path to test. the 'path' parameter is permitted to specify relative or absolute path information.

Exists() method return value data type is System.Boolean. if 'path' refers to an existing directory then method return 'true'. Exists() method return 'false', if directory does not exists or an error occurs when trying to determine if the specified directory exists.

.Net framework DirectoryInfo class DirectoryInfo.Exists property also allow us to get a value indicating whether the directory exists. in the bellow example code we applied this property to check directory existstance.

the following asp.net c# example code demonstrate us how can we determine whether a specified directory exists programmatically at run time in an asp.net application.
DirectoryExists.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html>

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        if (!this.IsPostBack)
        {
            Label1.Text = "Physical Application Path: " + Request.PhysicalApplicationPath;
            string appPath = Request.PhysicalApplicationPath.ToString();
            string directory1 = appPath + "Test";
            string directory2 = appPath + "TestFolder";
            Label1.Text += "<br /><br />Directory1: " + directory1;
            Label1.Text += "<br />Directory2: " + directory2;
        }
    }

    protected void Button1_Click(object sender, System.EventArgs e) {
        string dir1 = Request.PhysicalApplicationPath.ToString() + "Test";
        string dir2 = Request.PhysicalApplicationPath.ToString() + "TestFolder";
        
        DirectoryInfo path1 = new DirectoryInfo(dir1);
        bool path1Exists = path1.Exists;

        DirectoryInfo path2 = new DirectoryInfo(dir2);
        bool path2Exists = path2.Exists;

        Label2.Text = "Directory1 exists?: " + path1Exists.ToString();
        Label2.Text += "<br />Directory2 exists?: " + path2Exists.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to get whether the specified directory exists or not in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">asp.net example: directory exists?</h2>
        <asp:Label 
            ID="Label1" 
            runat="server" 
            Font-Size="Large" 
            ForeColor="DarkGreen"
            >
        </asp:Label>
        <br /><br />
        <asp:Label 
            ID="Label2" 
            runat="server" 
            Font-Size="Large" 
            ForeColor="Crimson"
            Font-Italic="true"
            >
        </asp:Label>
        <br /><br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Font-Bold="true" 
            ForeColor="SeaGreen"
            Text="Test Directory Exists?"
            OnClick="Button1_Click"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples