title: WPF accessing a resource from anywhere in the assembly using relative path
body: <p>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:</p>
  
  <ul>
  <li>Controls (folder)
  
  <ul>
  <li>MemberList (class)</li>
  </ul></li>
  <li>Resources  (folder)</li>
  <li>Windows (folder)
  
  <ul>
  <li>Window1.xaml</li>
  <li>Window2.xaml</li>
  <li>Helper windows (folder)
  
  <ul>
  <li>Window3.xaml</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>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:</p>
  
  <pre><code>new Uri("../Resources/my_image.png", UriKind.Relative)
  </code></pre>
  
  <p>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. </p>
  
  <p>If I change the Uri to this: (adding another "go back" command in the path)</p>
  
  <pre><code>new Uri("../../Resources/my_image.png", UriKind.Relative)
  </code></pre>
  
  <p>the image is being displayed in the Window3, but it is not displayed in the Window1 or Window2.</p>
  
  <p>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?</p>
  
  <p>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.</p>
  
  <p>My file structure looks like this:</p>
  
  <ul>
  <li>bin
  
  <ul>
  <li>Debug
  
  <ul>
  <li>MyApplication.exe</li>
  </ul></li>
  </ul></li>
  <li>Resources</li>
  </ul>
  
  <p>I was trying to use the path that everyone suggested:</p>
  
  <pre><code> new Uri("pack://application:,,,/../../my_image.png", UriKind.RelativeOrAbsolute)
  </code></pre>
  
  <p>but I get the exception:</p>
  
  <blockquote>
    <p>Cannot locate resource 'my_image.png'.</p>
  </blockquote>
  
  <p>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?</p>