Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uri relative vs absolute

Tags:

c#

uri

I have some trouble with uri in my C# code

This is working:

var uri = new Uri("d:/Programozas dotNet/Ugyfel/Ugyfel.ClientWpf/Skins/MainSkin.xaml");

But unfortunately non of these:

var uri = new Uri("/Skins/MainSkin.xaml", UriKind.Relative);
var uri = new Uri("pack://application:,,,/Skins/MainSkin.xaml");
var uri = new Uri("Ugyfel.ClientWpf;/Skins/MainSkin.xaml", UriKind.Relative);

IOException: Cannot locate resource 'skins/mainskin.xaml'.

How can I use relative uri insted of absolute?

like image 415
Oszkar Avatar asked Aug 30 '12 18:08

Oszkar


2 Answers

That was a stupid misstake. The correct method is without leading "/"

var uri = new Uri("Skins/MainSkin.xaml", UriKind.Relative);

Thank you for your effort

like image 65
Oszkar Avatar answered Oct 31 '22 04:10

Oszkar


Relative Url (in WPF or any other desktop application) is relative to Environment.CurrentDirectory. Usually this is the folder where your exe resides, but it can be different in VS Unit Testing environment.

I assume that in your project you have a folder called "Skins", and probably your exe is in "bin\debug".

The easiest thing to do is to set the MainSkin.xaml to be copied to the output directory (in the file properties) so you can refer to it with the name only with no path.

like image 4
Amiram Korach Avatar answered Oct 31 '22 04:10

Amiram Korach