Skip to content

Application.GetContentStream() may fail if the app is in a path that contains a "#". #4781

Closed

Description

@neelabo
  • .NET Core Version: .NET 5.0
  • Windows version: 21H1
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes

Problem description:

Application.GetContentStream() will fail if you place the executable in a path that contains "#".
Apparently, the paths after the "#" have disappeared.

Minimal repro:

For example, if you place the executable file and content file in "C:\FooBar\Path#Name\",
you will get the exception FileNotFoundException: Could not find file 'C:\FooBar\test.png'..

var uri = new Uri("/test.png", UriKind.Relative); // test.png is content file
var streamResourceInfo = Application.GetContentStream(uri); // <- Exception!!

Activity

  1. added a commit that references this issue on Jul 5, 2021
  2. lindexi commented on Jul 5, 2021

    @lindexi
    Member

    I wrote a demo in GitHub: https://github.com/lindexi/lindexi_gd/tree/e1022aededd1871017e4c43f228ad1fe26c99b57/ and I found that it seems is the Uri's bug, not the WPF

    But I do not think the code in wpf is all right, such as ContentFilePart.cs

    private Uri GetEntryAssemblyLocation()
    {
    Uri entryLocation = null;
    try
    {
    #pragma warning disable // Type or member is obsolete
    entryLocation = new Uri(Application.ResourceAssembly.CodeBase);
    #pragma warning restore // Type or member is obsolete
    }
    catch (Exception ex)
    {
    if (CriticalExceptions.IsCriticalException(ex))
    {
    throw;
    }
    // `Swallow any other exceptions to avoid disclosing the critical path.
    //
    // Possible Exceptions: ArgumentException, ArgumentNullException, PathTooLongException
    // DirectoryNotFoundException, IOException, UnauthorizedAccessException,
    // ArgumentOutOfRangeException, FileNotFoundException, NotSupportedException
    }
    return entryLocation;
    }

    The Application.ResourceAssembly.CodeBase will return to the path that contains a # and the URI will be created by this string.

  3. added this to the Future milestone on Jul 12, 2021
  4. added a commit that references this issue on Jul 23, 2021
  5. self-assigned this
    on May 11, 2022
  6. dipeshmsft commented on Jan 6, 2023

    @dipeshmsft
    Member

    Closing this issue as the corresponding fix has been merged in main branch

  7. ghost removed this from the Future milestone on Jan 6, 2023
  8. ghost locked as resolved and limited conversation to collaborators on Feb 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions