WPF accessing a resource from anywhere in the assembly using relative path

wpf uri relative path
wpf uri to resource
wpf icon relative path
wpf assets folder
wpf application resources
wpf resource binding
wpf embedded resource
wpf where to store images

In my application I am creating a ton of custom controls that I'm then using in different windows. The structure of my project looks something like this:

  • Controls (folder)
    • MemberList (class)
  • Resources (folder)
  • Windows (folder)
    • Window1.xaml
    • Window2.xaml
    • Helper windows (folder)
      • Window3.xaml

I am creating classes (controls) that I store in the Controls folder. If I want to use an image in the control, I access it like so:

new Uri("../Resources/my_image.png", UriKind.Relative)

Sometimes though, I want to use the same control in multiple windows and sometimes these windows are stored in different folders. If I use the control in say Window3 that is stored in another folder, the image is not displayed.

If I change the Uri to this: (adding another "go back" command in the path)

new Uri("../../Resources/my_image.png", UriKind.Relative)

the image is being displayed in the Window3, but it is not displayed in the Window1 or Window2.

Is there a way to create paths that are relative to the main executable and not relative to the window that is displaying the control?

EDIT: I forgot to mention one important part. Because of the fact that while starting this application I was just learning how to use WPF, I messed one thing. My resources folder is not in the folder that contains the main executable.

My file structure looks like this:

  • bin
    • Debug
      • MyApplication.exe
  • Resources

I was trying to use the path that everyone suggested:

 new Uri("pack://application:,,,/../../my_image.png", UriKind.RelativeOrAbsolute)

but I get the exception:

Cannot locate resource 'my_image.png'.

I could try to relocate the Resources folder, but it would mess up all other paths. Is there any way to go "back" in folder structure in the "pack" uri? If not, what would be the best way to relocate a folder so all paths do not get messed up?

Use a "pack" uri...so you would use:

pack://application:,,,/Resources/my_image.png

so like this:

new Uri("pack://application:,,,/Resources/my_image.png");

to refer to your "image" from ANY of your Windows/Controls.

If you will have other "projects"/"assemblies"...which you want to be able to refer to the resource then use:

pack://application:,,,/AssemblyNameContainingResource;component/Resources/my_image.png

And when you are adding your images in your Resource folder....make sure you use Resource as the Build Action.

Pro WPF in C# 2010: Windows Presentation Foundation in .NET 4, WPF has a solution that's workable, but it's not fully mature. element to the .​csprojfile for your project anywhere in the first <PropertyGroup> element: Inside that folder is a satellite assembly with the same name as your application and This assembly contains all the compiled BAML resources for your application, which  The pack URI for a resource file that is compiled into the local assembly uses the following authority and path: Authority: application:///. Path: The name of the resource file, including its path relative to the root of the local assembly project folder.

Use Pack Uri Scheme

Resource File Pack URIs - Local Assembly

The pack URI for a resource file that is compiled into the local assembly uses the following authority and path:

Authority: application:///.

Path: The name of the resource file, including its path relative to the root of the local assembly project folder.

The following example shows the pack URI for a XAML resource file that is located in the root of the local assembly's project folder:

pack://application:,,,/ResourceFile.xaml

The following example shows the pack URI for a XAML resource file that is located in a subfolder of the local assembly's project folder:

pack://application:,,,/Subfolder/ResourceFile.xaml

How to use relative path for image in WPF when image is not in , Though I am able to use images from the resource directory by using <Image set all images 'Build Action' to 'Resource', then I can access the image anywhere in the solution using (as long as the assembly is referenced): For reference, the Content option is one of the possible options to access a file in WPF. Other options include Resource (either embedded in the current assembly, or in another assembly of the same application) and Site of Origin to access a file which has no relationship with the current application: Source; Content type files are linked to the executable (assembly) only by the relative path they are stored.

Short answer: Use pack URI notation.

Example:

Your URI would look similar to: pack://application:,,,/Resources/my_image.png regardless of where in the XAML you use it.

Documentation:

MSDN - Pack URIs in WPF

Pack URIs, Assemblies are loaded out of the application folder or a special It seems like a natural fit for an editor - you write you want to push your document out to somewhere. Contains(".resources")) return null; // check for assemblies already need to have access to the WPF UI, there's no easy way to deal with  Absolute vs. Relative Pack URIs If a leading backslash is used, however, the relative pack URI reference is then considered relative to the root of the application . Therefore if you'd like the images to remain relative to your referenced assembly, simply omit the leading slash or use ../ if in a sub-folder.

In a submodule such as SubModuleA, in the viewmodel of SubModuelA, when you want to access the resource file of SubModuelA instead of the started project.

For example, the resource file's directory is StartApp/SubModuelA/Resource/Data/JsonData.json, In viewmodel of SubModuelA it's path should be

string jsonFile="../../../SubModuleA/Resource/Data/JsonData.json"

I validated this and wrote data into the jsonFile.

Loading .NET Assemblies out of Seperate Folders, assembly. You can use an assembly resource to make sure your application has an Assembly resources in a WPF application work in essentially the same way as the previous section referenced a resource using a relative URI, like this: □Tip When using an absolute URI, you can use a file path, a UNC path to a  Windows Presentation Foundation (WPF) offers special support for configuring, identifying, and using these types of data files, which are called application data files. This support revolves around a specific set of application data file types, including: Resource Files: Data files that are compiled into either an executable or library WPF

Resources, WPF accessing a resource from anywhere in the assembly using relative path. In my application I am creating a ton of custom controls that I'm then using in  In Generic.xaml, you cannot use resources defined in App.xaml as StaticResource. Only as DynamicResource. In Generic.xaml, you can use resources as StaticResource if defined in Generic.xaml or in a resource dictionary in Generic.xaml ‘s MergedDictionaries. As long as your default style is in Generic.xaml itself and not in a merged dictionary.

Wpf resource path, WPF accessing a resource from anywhere in the assembly using relative path. Apr 22, 2008 · Quick WPF Tip: How to bind to WPF application resources and  Download source - 42.5 KB; Introduction. Many developers have trouble with designer support of WPF styles, which are defined in another assembly, and should be referenced using DynamicResource and ComponentResourceKey.

Wpf path resources, WPF accessing a resource from anywhere in the assembly ▷ 8:25. The pack URI Trick To Use StaticResource With Path - TechNet Articles ▷ 7:93. WPF  assembly= The assembly that contains some or all of the referenced CLR namespace. This value is typically just the name of the assembly, not the path, and does not include the extension (such as .dll or .exe). The path to that assembly must be established as a project reference in the project file that contains the XAML you are trying to map.

Comments
  • Use a "pack" url....and refer to the component that contains the actual resource e.g.: pack://application:,,,/MyAssemblyOrApplicationName;component/Resources/my_image.png
  • Just in case, do not specify UriKind.Relative on a Pack URI. None of the three answers is actually giving you an appropriate example.
  • @Clemens Thank you, it is an important comment. Please look at the edited part of my question to find some additional info.
  • You haven't read the online documentation linked in the answers. pack://application:,,,/../../my_image.png is an invalid Pack URI. Besides that, better do not specify an UriKind at all.
  • Thank you for your response, please look at the edited part of my question to find some additional info.
  • I accepted this post as an answer because it provided the most information.
  • Thank you for your response, please look at the edited part of my question to find some additional info.
  • It should be new Uri("pack://application:,,,/Resources/my_image.png"); if Image stays in Resources folder of your project.
  • Thank you for your response, please look at the edited part of my question to find some additional info.