Xaml - Bitmap UriSource - absolute path works, relative path - no, why?

I, WPF and Xaml-noob, have worked for console applications for me to just provide a relative path to the executable to access the file. It doesn't seem to work in xaml for me. (code below)
The absolute path works fine.

Is it possible in XAML in a WPF application to access the file simply by using the relative directory path of your executable file as a valid UriSource? If so, how and if not, why?

I found the following question where they talked about adding a file through Visual Studio "Add an existing item", so this seems like another problem.

How to set WPF property BitmapImage UriSource relative path?

<Window.Icon> <!--absolute path works:--> <BitmapImage UriSource="C:\LongPath\SolutionFolder\ProjectFolder\bin\Debug\path4.ico" /> <!--none of the following relative paths worked:--> <!--AppDomain.CurrentDomain.BaseDirectory returns the Debug-folder--> <!--<BitmapImage UriSource="path4.ico" />--> <!--<BitmapImage UriSource="../path4.ico" />--> <!--<BitmapImage UriSource="Debug/path4.ico" />--> <!--<BitmapImage UriSource="bin/Debug/path4.ico" />--> <!--<BitmapImage UriSource="../bin/Debug/path4.ico" />--> <!--<BitmapImage UriSource="../../bin/Debug/path4.ico" />--> <!--<BitmapImage UriSource="../Debug/path4.ico" />--> </Window.Icon> 
+7
source share
2 answers

URIs can be misleading because they can reference both files on disk and resources in the application. Thus, the relative path may be for the resource in the application when you intend it to be on disk.

You can use siteoforigin authority to force the use of relative file URIs. This blog explains this more, but here is an example:

 pack://siteoforigin:,,,/path4.ico 
+11
source

Relative paths without qualifications look for the link file in application resources, if the path should be relative to the executable file, you can use pack://siteoforigin:,,,/path4.ico , see Pack URI on MSDN .

+4
source

All Articles