tags:

views:

39

answers:

2

I know next to nothing about the URI class. I need to make an Relative URI to reference a file in my project (that is on the root of the project).

How can I do that?

This is what I have tried and it does not seem to work:

var uri = new Uri("ModuleCatalog.xaml", UriKind.Relative)

I have also tried:

var uri = new Uri("/ShellProject;component/ModuleCatalog.xaml", UriKind.Relative);

When I examine my uri variable in the debugger it has thrown a lot of exceptions. The only value that is real is the original text value.

What do I need to do to make it a valid uri?

I think that I don't get how a Uri object works.

I guess I am asking for the basics of how to make a uri and have it reference a file in my project (with out having to hard code the full path from the C:\ drive.

A: 

What does "not work" mean? Both code examples compile and run, giving valid relative URIs.

Or perhaps you want to make absolute URIs from the relative URIs? That is, if your root is "http://example.com", you want to create "http://example.com/ModuleCatalog.xaml". In that case, use the Uri.TryCreate overload that lets you pass in the root. For example:

Uri baseUri = new Uri("http://example.com");
Uri newUri;
if (Uri.TryCreate(baseUri, "ModuleCatalogue.xaml", out newUri))
{
    // Uri created successfully
}
Jim Mischel
"Not work" means that "When I examine my uri variable in the debugger it has thrown a lot of exceptions" Also when I try to use it in the code it does find the file.
Vaccano
I am making a WPF appliaction that is not a website. Should I not be using a uri if this is not a website?
Vaccano
+1  A: 

Is your application a Web App? You don't use URIs to reference local files in non-web apps.

The expression

var uri = new Uri("ModuleCatalog.xaml", UriKind.Relative)

Doesn't throw any exceptions on construction, it throws later when it is used improperly. Since you mentioned you are developing a WPF app, if you want to locate this file you should use:

string assemblyLocation = Assembly.GetExecutingAssembly().Location
string moduleCatalogPath = Path.Combine(assemblyLocation, "ModuleCatalog.xaml");
Jader Dias