Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Check if a given url is file or directory?

Tags:

c#

path

I have a method which will input either a http , ftp or a local path. With the input url, i need to decide whether it is a file or directory.

Path.GetExtension(url) works almost fine. But if a directory begins with/have '.' in its name, then this checking will fail.

Is there any other methods to check and list url if directory ?

like image 548
csLijo Avatar asked Oct 29 '12 06:10

csLijo


2 Answers

You could use File.Exists(url) and Directory.Exists(url)

Another approach would be to create an array of extensions then check the result Path.GetExtension(url) against it.

like image 149
evanmcdonnal Avatar answered Sep 20 '22 06:09

evanmcdonnal


The following code takes the path, looks at the last substring (after the last /) and checks if there is a '.' in that substring to determine if it is a file or a path. isFile will be a boolean, true meaning that it is a file.

var isFile = new Uri(url).AbsolutePath.Split('/').Last().Contains('.');

like image 37
Alex from Jitbit Avatar answered Sep 19 '22 06:09

Alex from Jitbit