<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="CheckFile" OnClick="Button1_Click" style="height: 26px" />
<br />
<asp:Image ID="Image1" runat="server" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private bool FileExist()
{
string spath = System.Web.HttpContext.Current.Server.MapPath("~/upload");
string path = spath + "/picture.jpg";
FileInfo imageFile = new FileInfo(path);
bool fileExists = imageFile.Exists;
return fileExists;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileExist())
{
Image1.ImageUrl = "~/upload/picture.jpg";
}
else
{
Response.Write("<script>alert('Image not found')</script>");
}
}
}
Check if a file exists on the server in Mvc
//We need to convert the path to a physical path with Server.MapPath(relativePath)
if (File.Exists(Server.MapPath("~/upload/picture.jpg")))
{
}
File.Exists() method of the File class in the System.IO namespace. Using that method you can easily determine whether a file exists in the folder or not.
File.Exists() is the popular and simplest ways to check verify file existing or not in the folder. this returns true or false in the file depending on file exists or not.
We should not use File.Exists() to validate path, this method checks if the file specified in path exists file. And return an invalid path to Exists returns false.
The post How To Check Whether File Exists or Not in Asp.Net ? appeared first on Software Development | Programming Tutorials.
Read More Articles
- Write a value which contain comma to a CSV file in c#?
- Reading CSV File with cells containing commas c#
- [Simple Way]-How to get data from database using JQuery Ajax in asp net MVC
- [Simple Way]-ASP.NET Core Upload Multiple File Web API model
- [Easy Way]-Receive File and other form data together in ASP.NET Core Web API
- How to add new rows to an existing word document table in C#
- Implementing “Remember Me” Feature in ASP.NET MVC
- How to post File Upload and other form fields to one action Asp .Net MVC C#
- How To Post File and Data to API using HttpClient C#
- Sample AAC File Download For Testing
- Create ASP.NET Core Web API Without Entity Framework
- Difference Between EXCEPT and NOT IN
- Create Login,Signout and Registration in Asp .Net Mvc Using Entity
- Export Gridview to Excel and Csv in Asp .Net With Formatting Using c#
- How to Display Binary Image in Gridview from Database in Asp .Net c#
- [Solved]-How to Upload pdf file using jquery MVC?
- [Solved]-Uploading both data and files in FormData using Ajax MVC
- [Solved]-Download pdf file from link and save in local file folder in Asp .Net
- [Solved]-How Upload and Download PDF file Database in ASP.Net?
- [Solved]-Delete Files older than X Months,Days,Minute in a Directory- C# .NET
- [Solved]-FFMPEG Converted Mp4 Video Not Working In HTML5-C#
- How to Create Multilingual Website in Asp .Net
- How To Check Whether File Exists or Not in Asp.Net ?
- Insert delete update records in CSV file -ASP.NET
- Update an Image with Upload Button For Each Row in Gridview-Asp .Net
- Simple Way To Insert ,Update and Delete in Gridview-Asp.Net
- Login And Registration in Asp .Net MVC Without Entity Framework
- [Solved]- find records from one table which don’t exist in another Sql
- [Solved]-Adding a column with a default value to an existing table in SQL Server
- Download Sample .WAV File For Testing
- Download Sample 3gp Video file for Testing
- Download Sample PDF Files For Testing
- Download Free Sample Xml File With Multiple Records
- Download Free Sample Json File With Multiple Records
- Sample Video Download Files For Practice
- Sample Files Download
- Download Sample Excel File with Data for Analysis
- Download Sample .Docx and .Doc File for Testing
- Download Sample Csv File For Testing | .Csv Flies
- Download Sample xlsx and xls File with Data For Testing