System.Uri fails to give correct AbsolutePath and LocalPath if the Path contains “#”

前端 未结 3 740
太阳男子
太阳男子 2021-01-06 06:02

I have C# code that is trying to get the LocalPath for a executing assembly using the following line of code:

Uri uri = new Uri(Assembly.GetExecut

3条回答
  •  余生分开走
    2021-01-06 06:44

    I assume The URI functions are stripping away everything after the sharp # because it thinks its an anchor.

    URI are designed for identifying resources on the internet, so your # character would be an illegal character in the middle of any URI.

    Take even this question for example, the Title is

    System.Uri fails to give correct AbsolutePath and LocalPath if the Path contains “#”

    but the end of the URL has the "#" stripped away

    system-uri-fails-to-give-correct-absolutepath-and-localpath-if-the-path-contains

    Why do you need to convert it into a URI anyway. The only difference between these 3 console.writelines is the fact that the first two are prefixed with File:///

    Console.WriteLine(Assembly.GetExecutingAssembly().CodeBase);  // 1
    Uri uri = new Uri(Assembly.GetExecutingAssembly().CodeBase);
    Console.WriteLine(uri);                                       // 2
    Console.WriteLine(uri.LocalPath);                             // 3
    

提交回复
热议问题