11

Possible Duplicate:
Get file name from URI string in C#

How to extract file name from an Uri in C#?

for instance, I have a Uri

"http://audacity.googlecode.com/files/audacity-win-2.0.exe"

but how to extract the file name

"audacity-win-2.0.exe"

and save it as a string?

Thank you,

Jerry

Community
  • 1
  • 1
Jerry
  • 1,018
  • 4
  • 13
  • 22

4 Answers4

9

Path.GetFileName can do it:

Uri u = new Uri("http://audacity.googlecode.com/files/audacity-win-2.0.exe");
Path.GetFileName(u.AbsolutePath);
Twenty
  • 5,234
  • 4
  • 32
  • 67
Michael
  • 8,891
  • 3
  • 29
  • 42
  • This should be the accepted answer. If you check uri.IsFile will give you false because is not a local file. – Arnel Oct 21 '18 at 04:24
6

How about

Path.GetFileName(string path);

In your case

Path.GetFileName(new Uri("http://audacity.googlecode.com/files/audacity-win-2.0.exe").AbsolutePath);
Rhaokiel
  • 813
  • 6
  • 17
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
2

E.g.

Uri uri = new Uri("http://audacity.googlecode.com/files/audacity-win-2.0.exe");
string Path.GetFileName(uri.AbsolutePath);
Samuel Parkinson
  • 2,992
  • 1
  • 27
  • 38
penartur
  • 9,792
  • 5
  • 39
  • 50
0

It is not safe to assume that a URI will always represent physical files therefore extracting file name from a URI is not always guaranteed.

Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42