Path functions for URL

后端 未结 4 1449
盖世英雄少女心
盖世英雄少女心 2021-01-18 13:13

I want to use functions of Path class (GetDirectoryName, GetFileName, Combine,etc.) with paths in URL format with slash (/).

E

相关标签:
4条回答
  • 2021-01-18 13:13

    If the URI is a local file URI of the form file://whatever then you can call string path = new Uri(whatever).LocalPath and call the Path methods on it. If you cannot guarantee the Uri is to a local path, you cannot guarantee components of the Uri correspond to machines, folders, files, extensions, use directories, separator characters, or anything else.

    0 讨论(0)
  • 2021-01-18 13:18

    Have you considered using a combination of System.Uri, System.UriBuilder, and (if necessary) custom System.UriParser subclass(es)?

    0 讨论(0)
  • 2021-01-18 13:25

    For GetDirectoryName(), you can use

    pageRoot = uri.Remove(uri.LastIndexOf('/') + 1);
    
    0 讨论(0)
  • 2021-01-18 13:28

    I'm afraid GetDirectoryName, GetFileName, Combine,etc. use Path.DirectorySeparatorChar in the definition and you want Path.AltDirectorySeparatorChar.

    And since Path is a sealed class, I think the only way to go about is string replacement.You can replace Path.DirectorySeparatorChar('\') with Path.AltDirectorySeparatorChar('/') and Path.VolumeSeparatorChar(':') with ":/"

    0 讨论(0)
提交回复
热议问题