No wonder is does not work. I don't know where are you images and what is the directory you calculate in your code, but it cannot work in principle.
First, you use immediate constants of the type string everywhere. It is not supportable. These path names really belong in configuration files.
Now you calculate the image directory based on
System.Environment.CurrentDirectory
, then you find some sub-directory of it. This directory is actually a current directory which can be… anything at all. This is not a directory where you put your executable files (did you mean to assume that?); this is the directory where your user starts your application. It can be any valid directory. In one application start in could be one, next time it could be another.
The reliable way to find out executable directory is this:
string exePath = System.IO.Path.GetDirectroryName(
System.Reflection.Assembly.GetEntryAssembly().Location);
Now, are your image files read-only? You can use executable path reliable only if they are read-only. If not, you should use one of the "special directories" associated with the current user or all users. For this purpose, use
System.Environment.GetFolderPath(System.Environment.SpecialFolder)
. Most typically, you will use the argument
Environment.SpecialFolder.ApplicationData
,
Environment.SpecialFolder.CommonApplicationData
or
Environment.SpecialFolder.LocalApplicationData
.
See:
http://msdn.microsoft.com/en-us/library/14tx8hby.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx[
^].
Let's come back to the situation when your image files are read-only. Even better method to use them would be embedding them in your application in the form of resources (.resx). Don't add them to the project directly. Create a .resx resource, use "Add resource" => "Add existing file". Add your image files. They will be added to the project, and references to them will be added to your resource. Look at the properties of each file. They won't be copied to output. Instead, they will be embedded to your executable. To use them, take a look at auto-generated source code file, as a child node of your .resx file. If has a static class with some static properties. The names of these properties will be based on the names of your original image files. You won't need to read resource stream, use resource manager or anything like that. Just use those properties immediately. If, for example, you image files were in PNG format (recommended) you auto-generated properties will be of the type
Bitmap
. Those bitmaps are ready-to-use.
Additional benefit is: your user won't be able to modify your images. If you also sign your assembly (highly recommended), you will make modification of those embedded images practically impossible. It beats you directory approach, doesn't it?
—SA