00 Votes

C#/.NET: Check if a file exists

Question by CSchaf | 2014-09-05 at 19:38

How can I check whether a file exists on my hard drive or any other storage device using C# (C-Sharp, .NET Framerwork)?

I have a string containing the full file path and would like to check that. Is there anything like one of the FileExists-functions known from other programming languages? When trying to type this, the IDE cannot find such a function.

ReplyPositiveNegative
Avatar
0Best Answer0 Votes

You can find the function, you are searching for, in the class "File" (System.IO) providing some file handling functions.

Here is a small example:

string dat = @"C:\test.dat";

if (File.Exists(dat)) {
  // file exists
} else {
  // file does not exist
}

You can just pass a string with the file path to File.Exists and you get true or false as the return value depending on the file is existing or not.
2014-09-06 at 07:49

ReplyPositive Negative
Reply

Related Topics

C#/.NET: Check if Folder exists

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.