Converting a URI path to a relative file system path in .NET

后端 未结 4 782
故里飘歌
故里飘歌 2020-12-30 18:36

How do I convert an absolute or relative URI path (e.g. /foo/bar.txt) to a (segmentwise) corresponding relative file system path (e.g. foo\\bar.txt

4条回答
  •  长发绾君心
    2020-12-30 19:03

    I figured out this way to produce a full absolute file system path from a relative or absolute URI and a base path.

    With:

    Uri basePathUri = new Uri(@"C:\abc\");
    

    From a relative URI:

    string filePath = new Uri(basePathUri, relativeUri).AbsolutePath;
    

    From an absolute URI:

    // baseUri is a URI used to derive a relative URI
    Uri relativeUri = baseUri.MakeRelativeUri(absoluteUri);
    string filePath = new Uri(basePathUri, relativeUri).AbsolutePath;
    

提交回复
热议问题