1

I have some images stored in a folder:

C:\MyPrograms\VisualStudio\BirdPrograms\BirdTouchScreenBasic\BirdTouchScreenBasic\pigeonImages

I plan on using this program on a different computer to which I am creating the program.

I have an array of the images in a List<Image>

Is there a way to load the image from a relative path instead of an absolute path?

ie. instead of

Image.FromFile("C:/MyPrograms/VisualStudio/BirdPrograms/BirdTouchScreenBasic/BirdTouchScreenBasic/pigeonImages/pigeon5.png");

I could just write:

Image.FromFile("./pigeonImages/pigeon5.png");

Am I wrong in assuming that the file Visual Basic uses for running the program is in the top folder with the pigeonImages folder one folder down.

So on a command line I should be able to type:

C:\BirdTouchScreenBasic> cd pigeonImages

geedubb
  • 4,048
  • 4
  • 27
  • 38
smokeAndMirrors
  • 155
  • 1
  • 5
  • 18
  • Have you tried your assumption? Because it is correct... – Simon Whitehead Jan 09 '14 at 22:37
  • "Am I wrong in assuming" - yes, Easy enough to verify... – D Stanley Jan 09 '14 at 22:38
  • Is this WPF/WinForms? – Tieson T. Jan 09 '14 at 22:38
  • check this: http://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-a-net-console-application – kmalmur Jan 09 '14 at 22:38
  • 2
    Note that all relative paths are from [current directory](http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory%28v=vs.110%29.aspx) which can change while program is running or not necessary equal to location of executable. Duplicate question deals with files located next to executable which seem exactly your case. – Alexei Levenkov Jan 09 '14 at 22:53
  • @geedubb You are right. I guess I should have used the WinFroms tag when I was searching for my question. :) – smokeAndMirrors Jan 09 '14 at 23:06

2 Answers2

1

Based on the paths you've supplied if the current working directory is BirdTouchScreenBasic then @".\pigeonImages\pigeon5.png" would be a correct path. If you want these images to be accessible regardless of where the program runs you should add them to your solution and make it so they're copied to the output directory. If that is the case then you can use a relative path throughout your code without problems. If you make the paths @".\pigeonImages\fileName.ext" but don't actually structure your project so that folder and the files within it exist in the CWD then you're going to have problems.

If you don't know how to add the images a resource in your C# project here are basic steps; right click the proj file and select add then new folder, create the pigeonImages folder. Right click the folder and select add then new item. Navigate to the image file you want to add and select it, press ok. Right click the image you just added, select properties. There's is an option called "Copty to Output Directory" set that to Copy if newer or Copy Always.

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
1

Never do this, it is entirely too unpredictable what your Environment.CurrentDirectory will be set to. It is not under your control, whatever program starts your program controls it. And this goes wrong way too often.

Instead, you should create an absolute path that's based on the location of your EXE. With the assumption that you'll deploy the image files in the same directory as the EXE or in a subdirectory. Use code like this, spelled out for clarity:

using System.Reflection;
using System.IO;
...
    public static Image LoadPigeon(string name) {
        var exeDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        var imgDir = Path.Combine(exeDir, "pigeonImages");
        var imgPath = Path.Combine(imgDir, name);
        return Image.FromFile(imgPath);
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    AppDomain.CurrentDomain.BaseDirectory is probably better than Assembly.GetEntryAssembly().Location in the general case as discussed here: http://stackoverflow.com/a/6041505/13087. GetEntryAssembly() can return null in some cases (admittedly not in a WinForms app - which is the OP's situation). – Joe Jan 10 '14 at 06:54