How to find out if a file exists in C# / .NET?


Question

I would like to test a string containing a path to a file for existence of that file (something like the -e test in Perl or the os.path.exists() in Python) in C#.

1
168
12/21/2012 10:46:20 AM

Accepted Answer

Use:

File.Exists(path)

MSDN: http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx

Edit: In System.IO

280
9/2/2008 7:24:55 AM

System.IO.File:

using System.IO;

if (File.Exists(path)) 
{
    Console.WriteLine("file exists");
} 

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon