Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking the existence of a file using relative path

Tags:

c#

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"); 
like image 976
logeeks Avatar asked Oct 17 '12 19:10

logeeks


People also ask

What is relative path in file management?

A relative path is a path that describes the location of a file or folder in relative to the current working directory. It can be best used to refer to websites that are located on the same domain, ideally on certain sections of websites in which the documents never change relationships to each other.

What is a relative path give example?

A relative path is a way to specify the location of a directory relative to another directory. For example, suppose your documents are in C:\Sample\Documents and your index is in C:\Sample\Index. The absolute path for the documents would be C:\Sample\Documents.


2 Answers

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...)

like image 142
Mike Park Avatar answered Oct 11 '22 14:10

Mike Park


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).

like image 26
pkmiec Avatar answered Oct 11 '22 15:10

pkmiec