Equivalent function to IO.Path.GetFileName for urls?

前端 未结 4 1199
情歌与酒
情歌与酒 2021-01-11 13:54

In .NET 4.0, whats the equivalent function to IO.Path.GetFileName for urls?

4条回答
  •  悲哀的现实
    2021-01-11 14:37

    The Uri class is your friend.

    Provides an object representation of a uniform resource identifier (URI) and easy access to the parts of the URI.

    IsFile will try to determine if the Uri does indeed point to a file.

    Use the Segements property in order to get the file name (it will be the last segment).

    Uri uri = new Uri("http://example.com/title/index.htm");
    var filename = uri.Segments[uri.Segments.Length - 1];
    // filename == "index.htm"
    

提交回复
热议问题