Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File Exists in WPF

Tags:

c#

if (!File.Exists("SomeFile.exe"))
{
//Does not exists
}

I have SomeFile.exe in the same path as the exe but the result is Does not Exists.

This does not happen in Windows Form, does something change?

like image 653
Athiwat Chunlakhan Avatar asked Aug 31 '09 08:08

Athiwat Chunlakhan


People also ask

How do I check to see if a file exists?

To check whether a Path object exists independently of whether is it a file or directory, use my_path. exists() .

How do you check if a file exists in a specific folder?

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

How do you create a file if not exist in C#?

For creating a directory, we must first import the System.IO namespace in C#. The namespace is a library that allows you to access static methods for creating, copying, moving, and deleting directories.


2 Answers

Try this to get the file in the executables directory.

string directory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string filePath = Path.Combine(directory, "SomeFile.exe");

if (!File.Exists(filePath))
{
    // 1337 code here plx.
}
like image 81
Patrik Svensson Avatar answered Nov 11 '22 18:11

Patrik Svensson


If your testing it from VS then the current directory is the Project dir not the release/debug folder (where your exe is)

like image 45
RvdK Avatar answered Nov 11 '22 19:11

RvdK