Check whether a file exists
The File class’s Exists() method allows us to determine whether the
specified file exists. File.Exists(path) method is under the System.IO
namespace. So we need to include the System.IO namespace on our page before
using this method. The Fileclass’s Exists() method needs to pass a parameter
named 'path'.
The path parameter type is a String and it represents the file to check. The path value contains the full path of a file including both folder location and file name with extension. The method returns a Boolean value. If it returns 'True' then the file exists in the web server otherwise, the file does not exist.
If the path parameter describes a directory then the method returns False. Because this method can only determine file existence not for use to check whether a directory exists. This method also returns False, if any error occurs while trying to determine file existence. This method should not be used for path validation. To check whether the path contains any invalid characters, we can call the GetInvalidPathChars() method.
The following asp.net c# example code demonstrates to us how we can check whether a file exists or not in the web server file system.
The path parameter type is a String and it represents the file to check. The path value contains the full path of a file including both folder location and file name with extension. The method returns a Boolean value. If it returns 'True' then the file exists in the web server otherwise, the file does not exist.
If the path parameter describes a directory then the method returns False. Because this method can only determine file existence not for use to check whether a directory exists. This method also returns False, if any error occurs while trying to determine file existence. This method should not be used for path validation. To check whether the path contains any invalid characters, we can call the GetInvalidPathChars() method.
The following asp.net c# example code demonstrates to us how we can check whether a file exists or not in the web server file system.
FileExists.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)
{
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>


