1

I have a Windows Forms project. I have a Resources folder and I wan to use the files there using relative path. Here is a printscreen of my project tree

enter image description here

As you may see I have folder UserControls where I have FileExplorer.cs it contains aa openFileDialog + pictureBox. I use this control in some of my forms which are in Forms folder. The case is that in Resources folder I have this T380.jpg image that I want to load by default but for now I can do it only by inserting the full path to it. Here is my code where I try to load the image:

 private void FileExplorer_Load(object sender, EventArgs e)
        {
            pictureBox1.ImageLocation = @"ShoesUnlimitedAdmin\Resources\T380.jpg";
            pictureBox1.Load();
        }

I use the Load event of the user control to load my image but it only works when I set the full path to the image like C:\\... and so. How can I point to the Resources folder of the project using relative path?

Leron
  • 9,546
  • 35
  • 156
  • 257
  • What are you setting the `BuildAction` to? – paul Feb 04 '13 at 13:55
  • It's MDI form, so if I get your question right, the answer should be the MDIparent which isi `Main_Form` in my `Forms` directory. – Leron Feb 04 '13 at 13:57
  • Nope - right click the T380.jpg file in solution explorer, choose `Properties...` and each file has a `BuildAction` property. This decides what VS does with the file when you build. Probably should be set to `Content` rather than `Resource` (http://stackoverflow.com/questions/145752/what-are-the-various-build-action-settings-in-vs-net-project-properties-and-wh) – paul Feb 04 '13 at 13:59
  • My bad, I'm new sorry. You are right it's set to `Content`. Do I have to change it? And how to build my relative path? – Leron Feb 04 '13 at 14:00
  • And it's set to `Copy if Newer` or `Copy Always`? This will decide if the files are copied to a 'Resources' sub folder of the bin/debug or bin/release folders. – paul Feb 04 '13 at 14:05
  • It's set to `Do not copy`. – Leron Feb 04 '13 at 14:08
  • If it's set to `Do not copy` then the jpg file will not be copied to your `bin` folders, this is likely why you cannot find it with a relative path. – paul Feb 04 '13 at 14:09
  • So do you have any suggestion how to fix this. I need to use relative path for this? – Leron Feb 04 '13 at 14:12
  • Depends, if you are going to embed them, change the BuildAction to 'Resource' or add the pictures through the Resource Editor. If you don't want to embed them, change the 'Copy to output folder' property to `Copy Always` or `Copy if newer`. – paul Feb 04 '13 at 14:16

1 Answers1

2

If these images are small then favor adding them as resources in the executable file so you can use Properties.Resources in your code and don't have to deploy the files on the user's machine. Use Project + Properties, Resources. Click the arrow on the "Add Resource" button and select Add Existing File.

If they are big (more than a couple of megabytes) then you'll indeed want to deploy them as separate files. You can find them back by using the location of the EXE program, here's a helper method, spelled out for clarity:

    public static string GetResourcePath(string filename) {
        string exepath = System.Reflection.Assembly.GetEntryAssembly().Location;
        string exedir = System.IO.Path.GetDirectoryName(exepath);
        string resdir = System.IO.Path.Combine(exedir, "Resources");
        return System.IO.Path.Combine(resdir, filename);
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • They are small less than 300kb – Leron Feb 04 '13 at 14:14
  • Hans, I have a similar issue in my application. Some of my image files are generated at run-time and thus cannot be set as project resources. I have verified that they are created where they should be in the `Debug` folder. Can you provide any insight as to why relative paths can only be used with files that have been marked as resources? – Jim Apr 15 '13 at 19:52