asp.net example  

How to check whether a file exists or not in asp.net



How to check whether a file exists or not in asp.net

FileExists.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        if (!this.IsPostBack)
        {
            TextBox1.Text += Request.PhysicalApplicationPath + "HP2133.jpg";
        }
    }

    protected void Button1_Click(object sender, System.EventArgs e) {
        string filePath = Request.PhysicalApplicationPath + "HP2133.jpg";

        FileInfo imageFile = new FileInfo(filePath);
        bool fileExists = imageFile.Exists;
        Label1.Text = "File exits?: " + fileExists.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to check whether a file exists or not in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">asp.net example: file exists</h2>
        <asp:Label 
            ID="Label1" 
            runat="server" 
            Font-Size="Large" 
            ForeColor="HotPink"
            >
        </asp:Label>
        <br /><br />
        <asp:Label 
            ID="Label2" 
            runat="server" 
            Text="File"
            ForeColor="DarkOliveGreen"
            Font-Bold="true"
            >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1" 
             runat="server"
             BackColor="DarkOliveGreen"
             ForeColor="AliceBlue"
             >
        </asp:TextBox>
        <br /><br />
        <asp:Button 
            ID="Button1" 
            runat="server" 
            Font-Bold="true" 
            ForeColor="SaddleBrown"
            Text="Check File Exists?"
            OnClick="Button1_Click"
            />
    </div>
    </form>
</body>
</html>
   








Related asp.net example