122

I need to check if an xml file exists in the folder.

DirectoryInfo di = new DirectoryInfo(ProcessingDirectory);
FileInfo[] TXTFiles = di.GetFiles("*.xml");
if (TXTFiles.Length == 0)
{
    log.Info("no files present")
}

Is this the best way to check a file exists in the folder.

I need to check just an xml file is present

4

8 Answers 8

211

This is a way to see if any XML-files exists in that folder, yes.

To check for specific files use File.Exists(path), which will return a boolean indicating wheter the file at path exists.

2
  • 4
    You can also use the FileInfo.Exists property
    – VMAtm
    Commented Sep 12, 2011 at 8:46
  • 13
    Noe that this answer returns false if the user does not have permission to read the file. So it does more than just checkinf if the file exists in a folder. You might want to use DirectoryInfo.GetFiles() and enumerate the result.
    – ogborstad
    Commented Feb 10, 2015 at 8:50
40

Use FileInfo.Exists Property:

DirectoryInfo di = new DirectoryInfo(ProcessingDirectory);
FileInfo[] TXTFiles = di.GetFiles("*.xml");
if (TXTFiles.Length == 0)
{
    log.Info("no files present")
}
foreach (var fi in TXTFiles)
    log.Info(fi.Exists);

or File.Exists Method:

string curFile = @"c:\temp\test.txt";
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
1
  • 5
    The DirectoryInfo and FileInfo classes are awesome. They offer lots of methods for dealing with these file system constructs, expose information in properties that are useful when binding to UI, and are serializable, so you can use them in configurations.
    – user1228
    Commented Nov 22, 2011 at 16:31
34

To check file exists or not you can use

System.IO.File.Exists(path)
9

Since nobody said how to check if the file exists AND get the current folder the executable is in (Working Directory):

if (File.Exists(Directory.GetCurrentDirectory() + @"\YourFile.txt")) {
                //do stuff
}

The @"\YourFile.txt" is not case sensitive, that means stuff like @"\YoUrFiLe.txt" and @"\YourFile.TXT" or @"\yOuRfILE.tXt" is interpreted the same.

1
  • if (File.Exists(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + @"YourFile.txt")) {
    – kkasp
    Commented Jan 12, 2023 at 7:45
7

This way we can check for an existing file in a particular folder:

 string curFile = @"c:\temp\test.txt";  //Your path
 Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
3

It can be improved like so:

if(Directory.EnumerateFileSystemEntries(ProcessingDirectory, "*.xml").ToList<string>().Count == 0)
    log.Info("no files present")

Alternatively:

log.Info(Directory.EnumerateFileSystemEntries(ProcessingDirectory, "*.xml").ToList<string>().Count + " file(s) present");
1
if (File.Exists(localUploadDirectory + "/" + fileName))
{                        
    `Your code here`
}
1
  • 4
    Although this code might (or might not) solve the problem, a good answer always requires an explanation on what this code does. Also note, that your answer doesn't seem to add anything new. You should also format your code sample appropriately and explain what localUploadDirectory is or why your answer even refers to it.
    – BDL
    Commented Aug 20, 2018 at 13:02
0

This woked for me.

file_browse_path=C:\Users\Gunjan\Desktop\New folder\100x25Barcode.prn
  String path = @"" + file_browse_path.Text;

  if (!File.Exists(path))
             {
      MessageBox.Show("File not exits. Please enter valid path for the file.");
                return;
             }

Not the answer you're looking for? Browse other questions tagged or ask your own question.