简体   繁体   中英

Checking the existence of a file using relative path

What is the best way to check the existence of a file using relative path.

I've used the following method but it returns false despite the fact that file is existing.

 bool a = File.Exists("/images/Customswipe_a.png");

That's not a relative path. You need to leave off the first / otherwise it will be interpreted as being rooted (ie C:/images...)

I guess that you are running this code in asp.net application, thats why you get false.

In asp.net you should use Server.MapPath("/images/Customswipe_a.png") to get "correct" path (relative to the web application root directory). Otherwise you get path local to the webserver executable (IIS/WEBDAV/..name any other).

The relative path is relative to the current working directory. It may not be the application directory. Call GetCurrentDirectory() to check the actual path you are testing.

You just need to define what your file is relative to

  • Your application main assembly?
  • Current directory?
  • Application data directory?
  • name it...

In each of these cases I'd suggest you to convert it into an absolute path by Path.Combine method:

public static readonly string AppRoot = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

...

//calling with a '/' heading makes the path absolute so I removed it
var fullPath = Path.Combine(AppRoot, "images/Customswipe_a.png");
var exists = File.Exists(fullPath);

This way you can guarantee where you are looking for. Even the Open/Save file dialogs may change your current directory. So, calling File.Exists without full path is usually a wrong decision.

You can test this path with System.IO.DirectoryInfo:

DirectoryInfo info = new DirectoryInfo("/images/Customswipe_a.png");
string absoluteFullPath = info.FullName;

As Mike Park correctly answered this path is likely to be (ie C:/images...)

The relative path, is a relative to something. In this API, it will, according to the documentation File.Exists :

Relative path information is interpreted as relative to the current working directory.

So everything here is depends what is CurrentDirectoty at the moment of execution of this query.

Plus, your path is not valid Desktop path (I assume you pick it from some web file, or knowledge). To understand if specified path contains not valid characters use GetInvalidCharacters function.

In your specific case it would be enough to use @"\\images\\Customswipe_a.png" .

The path is relative to the location of your binary file. In the case of a visual studio project, this would be %PROJECTDIR%/bin/(RELEASE||DEBUG)/

What I would do is put the filesystem root in a config file, and use that for your relative path.

In a WinForms application you can get the directory of the exe file with

string directory =
    Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);

Another solution uses Reflection

string directory =
    Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

As of .NET Core 3.1 , this works:

Use dependency injection to include service IWebHostEnvironment

private IWebHostEnvironment Env;

public MyController (IWebHostEnvironment env){
    Env = env;
}

Then use it to get path to root of app with Env.ContentRootPath

And combine it all:

var file = System.IO.Path.Combine(Env.ContentRootPath, "images", "some-file.png");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM