tags:

views:

167

answers:

7

I am developing an application in C#. I have a folder called myfolder it contains a file called mypoints.bmp. The folder myfolder is in my project folder it is in D drive. The path is D:\Myproject\myfolder\mypoints.bmp.

Now, in my program, whereever I need mypoints.bmp I have hardcoded the whole path. When my project is copied in to a different system under C drive, I am not able to run my project because of the hardcoded path. How can I give the path so that there will not be any problem even if it is loaded in to a different system under a different drive.

+7  A: 

The best would be to store the path in a configuration file.

Add -> New Item -> Application Configuration File

Then you can use the following class to pull the value you're looking for:

System.Configuration.ConfigurationManager

Have a look here on the usage.

Kyle Rozendo
A Settings file would be more appropriate here. The application configuration file is often stored in a read-only location, where the Settings files are intended to be configured per-user.
280Z28
+2  A: 

If the BMP file is used in the code and is accessed in runtime, there is no point in keeping it just in the project directory. it should be also present in the outputdirectory.

i.e you could write a post-build command to copy your folder to the output directory.

like copy "$(ProjectDir)\MyFolder\*.*" "$(OutDir)" then in the code you could just write the relative path i.e "MyFolder\MyPoints.bmp"

But please remember one thing. If this BMP file is not going to change through out your program execution, it is better that you put it as a resource.

SysAdmin
You don't need a post-build event to copy a file. Right-click on the file within vs solution explorer and choose properties. Set it to `Content` and `Copy if newer`.
Sam
oh yeah....nice option. but you dont have that option for a folder. only for files.
SysAdmin
@SysAdmin: If you create a folder with the desired name in your project and set the content flag of a file within this folder, VS will create this structure in your output folder too.
Oliver
I admit I didn't know that. glad that I learned a new thing today.
SysAdmin
+2  A: 

You can use something like GetCurrentPath(), which will return your .exe file path, then add to this path your \myfolder\mypoints.bmp. This will not be depend on C or D or ... drive and folder

Davit Siradeghyan
A: 

You can also use predefined or existed system variable. Anyway you must decide when the user (or you) have to define that path (in config file or wherever) - during instalaltion, first run, before first run or in any time when app is running.

raf
+3  A: 

If the images you are referring to are non-changing and are simply acting as a resource for your application then you could consider embedding them in resource files which are compiled as part of your assembly. That way you need never worry about paths or copying them.

Dan Diplo
A: 

Personally I would use the Environment class in C#. Then I will locate the local app settings folder using the Environment.SpecialFolder enum.

I will then store all my applications specific files inside a folder in the local app settings folder.

This way you are also UAC safe, since UAC will complain if you want to store files in Program Files if you are not Administrator.

Marthinus
+1  A: 

There is one path that you can always reliably find, no matter how your program got deployed to the target machine: the directory in which your .exe is stored.

Take advantage of this, put the .bmp file in the same directory. Or a subdirectory of the install directory. You didn't say what kind of program you wrote, it is easy with Application.ExecutablePath in Windows Forms. But the generic solution that works everywhere:

public static string GetImagePath(string filename) {
  string exeFile = System.Reflection.Assembly.GetEntryAssembly().Location;
  string exePath = System.IO.Path.GetDirectoryName(exeFile);
  string imgPath = System.IO.Path.Combine(exePath, @"images");
  string imgFile = System.IO.Path.Combine(imgPath, filename);
  return imgFile;
}

Which assumes the images are stored in a subdirectory named "images". Tweak as necessary.

Last but not least: don't forget that you can add images to your program's resources so it is baked in the .exe file. Project + Properties, Resources tab. Highly recommended.

Hans Passant