Home > Enterprise >  How to create a relative path using System.Uri
How to create a relative path using System.Uri

Time:01-19

I tried to create a URI like this

Uri uri = new Uri("file:///Pages/Menu/Page.xaml");

So I have a WPF project and its path is like this: C:/Programing/MyProjectName/ Now the path to my file are C:/Programing/MyProjectName/Pages/Menu/Page.xaml as you saw above the path I give to the Uri is file:///Pages/Menu/Page.xaml when I run this I get the error can't find the part of the path C:/Pages/Menu/Page.xaml which I expect because as I said above my files path is C:/Programing/MyProjectName/Pages/Menu/Page.xaml so my question is, is there any way to use a relative path in my Uri, I want to use a relative path because my app will not always be in the same folder.

Incase your wondering why I'm using a Uri is because I'm setting the Source property on a WPF frame and it uses Uri instead of string.

CodePudding user response:

In order to load a Page, use a Resource File Pack URI:

var uri = new Uri("pack://application:,,,/Pages/Menu/Page.xaml");

Which could also be written like this:

var uri = new Uri("/Pages/Menu/Page.xaml", UriKind.Relative);
  •  Tags:  
  • Related