How to check if a file exists in a folder?

C#XmlFileFileinfo

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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.");

Solution 3 - C#

To check file exists or not you can use

System.IO.File.Exists(path)

Solution 4 - C#

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.

Solution 5 - C#

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.");

Solution 6 - C#

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");

Solution 7 - C#

if (File.Exists(localUploadDirectory + "/" + fileName))
{                        
    `Your code here`
}

Solution 8 - C#

>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;
             }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser386258View Question on Stackoverflow
Solution 1 - C#CodeCasterView Answer on Stackoverflow
Solution 2 - C#VMAtmView Answer on Stackoverflow
Solution 3 - C#62071072SPView Answer on Stackoverflow
Solution 4 - C#Bennett YeoView Answer on Stackoverflow
Solution 5 - C#HimanszView Answer on Stackoverflow
Solution 6 - C#WonderWorkerView Answer on Stackoverflow
Solution 7 - C#BhavinView Answer on Stackoverflow
Solution 8 - C#ranojanView Answer on Stackoverflow