0

I have the following URL:

www.mysite.com/type/text.gif
www.mysite.com/type/video.gif
www.mysite.com/gettype.php?giveme=text.gif
www.mysite.com/gettype.php?giveme=video.gif

I have the following code to extract the file name from url:

var type = System.IO.Path.GetFileNameWithoutExtension("www.mysite.com/type/text.gif"); // text
type = System.IO.Path.GetFileNameWithoutExtension("www.mysite.com/type/video.gif"); // video
type = System.IO.Path.GetFileNameWithoutExtension("www.mysite.com/gettype.php?giveme=text.gif"); // gettype.php?giveme=text
type = System.IO.Path.GetFileNameWithoutExtension("www.mysite.com/gettype.php?giveme=video.gif"); // gettype.php?giveme=video

On the first two links I got the expected result - the file name but on the last two results I don't get the expected result, How can I got the file name from any url?

d219
  • 2,707
  • 5
  • 31
  • 36
  • 10
    `Path.GetFileNameWithoutExtension` is for *files*, not URLs. I suggest you use the `System.Net.Uri` class to get the path from the URL, *then* you can call `GetFileNameWithoutExtension` based on just the path part. – Jon Skeet Aug 18 '20 at 13:19
  • Does this answer your question? [Get file name from URI string in C#](https://stackoverflow.com/questions/1105593/get-file-name-from-uri-string-in-c-sharp) – d219 Aug 18 '20 at 14:07
  • With the use of ```System.Net.Uri``` you easily get "gettype.php" as filename. But to get "text.gif" you have to split the ```Query```. – M. Bauer Aug 18 '20 at 14:19

0 Answers0