Reputation: 4979
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");
Upvotes: 28
Views: 46190
Reputation: 20061
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");
Upvotes: 0
Reputation: 706
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 (i.e. C:/images...)
Upvotes: 3
Reputation: 2694
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).
Upvotes: 10
Reputation: 1432
You just need to define what your file is relative to
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.
Upvotes: 5
Reputation: 112259
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);
Upvotes: 0
Reputation: 62248
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"
.
Upvotes: 1
Reputation: 9726
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.
Upvotes: 6
Reputation: 12375
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.
Upvotes: 0
Reputation: 10931
That's not a relative path. You need to leave off the first /
otherwise it will be interpreted as being rooted (i.e. C:/images...)
Upvotes: 14