Share Your Source Code or Article

Do you have source code, articles, tutorials, web links, and books to share? You can write your own content here. You can even have your own blog.

Submit now...

Database Programming Made Easy

This tutorial will teach you step by step on how to connect and manipulate database. If you'd like to suggest a tutorial please write a comment at the bottom of this article.

Read more...

Hire Us to Do Your Work

Do you want a customized system? Do you want to setup your own website to do business? Then we are here to help you in your programming needs.

Read more...

Search

Check Existence of File in ASP.NET and C#
planetsourcecode's picture
Language: .NET
Category: Miscellaneous

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;

  1. Using system.IO;

We have to place these controls on the webpage (ASPX page)

  1. Text box with ID=Textbox1
  2. Button with ID= button1
  3. 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

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.IO;
  11.  
  12. public partial class _Default : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. if (IsPostBack)
  17. doesFileExist(TextBox1.Text);
  18. }
  19.  
  20. public void doesFileExist(string searchString)
  21. {
  22. if (TextBox1.Text != "")
  23. {
  24. string imageFolder;
  25. imageFolder = Server.MapPath("/media/") + searchString.ToString();
  26. if (File.Exists(imageFolder))
  27. {
  28. Label1.Text = "File <b>" + searchString + "</b> <u>does</u> exist in '/media/' folder.";
  29. }
  30. else
  31. {
  32. Label1.Text = "File <b>" + searchString + "</b> <u>does not</u> exist in '/media/' folder.";
  33. }
  34. }
  35. else
  36. Label1.Text = "Please enter some text.";
  37. }
  38. }

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

0
Your rating: None



Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <java>, <java5>, <javascript>, <mysql>, <php>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • Links to specified hosts will have a rel="nofollow" added to them.

  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Step by Step Java Tutorial

In this tutorial you will learn how to program with Java. It has a rich of information to be educated well with Java.

Read more...

Do You Have Question?

Do you have any question related to computer programming? Visit our forum and post new topic on the category you like. Be gentle when asking a question.

Ask now...

Point of Sale

Point of Sale is very useful especially for supermarkets or restaurants. I have included a barcode scanner in this program. Please check it out.

Read more...