Check whether a file exists
File.Exists() method allow us to determine whether the specified file exists. File.Exists(path) method is
in under System.IO namespace. so we need to include System.IO namespace in our page before using this method.
File.Exists() method need to pass a parameter named 'path'.
path parameter type is System.String and its represent the file to check. path value contains the full path of a file including both folder location and file name with extension. the method return a Boolean value. if it returns 'True' then the file is exists in web server, otherwise file is not exists.
if the path parameter describe a directory then the method return False. because this method can only determine file existence not for used to check whether a directory exists. this method also return False, if there are 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 demonstrate us how can we check whether a file exists or not in web server file system.
path parameter type is System.String and its represent the file to check. path value contains the full path of a file including both folder location and file name with extension. the method return a Boolean value. if it returns 'True' then the file is exists in web server, otherwise file is not exists.
if the path parameter describe a directory then the method return False. because this method can only determine file existence not for used to check whether a directory exists. this method also return False, if there are 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 demonstrate us how can we check whether a file exists or not in 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>


