C# File Exists

C# File Exists


C# file Exists

A very simple and very useful static method File.Exists
determines whether the file exists at specified path or not.
It is provided in System.IO namespace

Usage

 
Many times we require to check existence of file before user upload it on server via
web interface or applications. To avoid overwriting or modifying existing files it’s
better to check the existence of file with same name in specified directory.

Example

using System;
using System.IO;

namespace ConsoleAppication
{
    class Program
    {
        static void Main()
        {

             if( File.Exists(@"c:\SQLManagementStudio_x86_ENU.exe"))
             {
                 Console.Write("The file exixts");    
             }
             else
             {
                 Console.WriteLine("The file doesnot exists");
             }
        }
    }
}

Output: The file exixts

Description
 The File.Exists method accepts fully qualified path of the file as string type a parameter.
If file exists it returns Boolean true and if file doesn't exists it returns Boolean false.
The File.Exists method shold not be used for path validation.
Passing invalid path to Exists method returns false.

Note:
The File.Exists method should not be used for path validation.
Passing invalid path to Exists method returns false vale.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5