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.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,086 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,822 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,266 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 48,861 Reputation points Microsoft Vendor
    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.