Check Existence of File in ASP.NET and C#

We have to check if file exists before doing some things with that particular file. It will prevent error and work flow will be smooth, for this we have to include required namespace i.e system.IO; Using system.IO; We have to place these controls on the webpage (ASPX page) Text box with ID=Textbox1 Button with ID= button1 Label with ID= label1 The logic behind the application will check to see if there is any text in the textbox, this will be like validation check, when the button is clicked, first and foremost. If there is text present, it will check to see if this text is a file in the given folder. The user will be notified if it exists as a file or not. The source codes for whole page will be given below using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) doesFileExist(TextBox1.Text); } public void doesFileExist(string searchString) { if (TextBox1.Text != "") { string imageFolder; imageFolder = Server.MapPath("/media/") + searchString.ToString(); if (File.Exists(imageFolder)) { Label1.Text = "File " + searchString + " does exist in '/media/' folder."; } else { Label1.Text = "File " + searchString + " does not exist in '/media/' folder."; } } else Label1.Text = "Please enter some text."; } } About the author: PlanetSourceCode.in is a place for all developer providing free source codes, articles, complete projects,complete application in PHP, C/C++, Javascript, Visual Basic, Cobol, Pascal, ASP/VBScript, AJAX, SQL, Perl, Python, Ruby, Mobile Development

Add new comment