Reputation: 70344
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#.
Upvotes: 251
Views: 367673
Reputation: 129
I use WinForms and my way to use File.Exists(string path) is the next one:
public bool FileExists(string fileName)
{
var workingDirectory = Environment.CurrentDirectory;
var file = $"{workingDirectory}\{fileName}";
return File.Exists(file);
}
fileName must include the extension like myfile.txt
Upvotes: 1
Reputation: 157
Give full path as input. Avoid relative paths.
return File.Exists(FinalPath);
Upvotes: 9
Reputation: 58714
using System.IO;
if (File.Exists(path))
{
Console.WriteLine("file exists");
}
Upvotes: 80
Reputation: 6480
Use:
File.Exists(path)
MSDN: http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
Edit: In System.IO
Upvotes: 381