9

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 ?

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

File.Exists(url) is not going to work, as it will return false for http/ftp cases. As an alternative, i can consider your second approach.. Thanks evanmcdonnal
4

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('.');

2 Comments

While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
@KurtVandenBranden sometimes the code is self-explanatory (for those who can read it). Not every piece of code deserves this, otherwise you're just being formal, not helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.