AppDomain.CurrentDomain.BaseDirectory is Empty While Loading Media In WPF

2025-01-27T04:47:18.25+00:00

Hello,

I am working on a WPF window which supports multiple frameworks

<TargetFrameworks>net8.0-windows;net48</TargetFrameworks>

i have a media element in a window written

<MediaElement x:Name="Video"
                          LoadedBehavior="Manual" MaxHeight="450" MaxWidth="450"
                          Grid.Row="1"  Grid.Column="0" Grid.ColumnSpan="3"  VerticalAlignment="Center" HorizontalAlignment="Center" >
    <b:Interaction.Behaviors>
        <local:MediaElementBehavior MediaSource="{Binding Video}" />
    </b:Interaction.Behaviors>
</MediaElement>

while creating URI for the MediaSource i am getting URI is empty exception, upon stepping into the library and debugging i found out that

PresentationCore.dll!System.Windows.Media.MediaPlayerState.OpenMedia(System.Uri source) Line 589

if (source != null)

{
Uri baseDirectory = SecurityHelper.GetBaseDirectory(AppDomain.CurrentDomain);

Uri absoluteUri = ResolveUri(source, baseDirectory);

text = DemandPermissions(absoluteUri);
}

in the above code which resides in System.Windows.Media.MediaPlayerState , AppDomain.CurrentDomain.BaseDirectory is an empty string.

Note : System.Reflection.Assembly.GetExecutingAssembly().Location gives the working location but MediaPlayerState.cs library uses AppDomain.CurrentDomain.BaseDirectory is empty.

Developer technologies | Windows Presentation Foundation
Developer technologies | .NET | Other
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2025-01-28T07:23:30.18+00:00

    Hi @Bhat Sanjay Subrahmanya (SX/ETM12-EM) , Welcome to Microsoft Q&A,

    You can try to use AppContext.BaseDirectory instead of AppDomain.CurrentDomain.BaseDirectory in .Net 8.0.

    Uri baseDirectory = new Uri(AppContext.BaseDirectory);
    Uri absoluteUri = ResolveUri(source, baseDirectory);
    

    You can use AppContext.BaseDirectory for URI resolution by modifying the MediaElementBehavior or related binding code.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.