开发者

Hard Coded Paths in a .NET Program

开发者 https://www.devze.com 2022-12-25 23:43 出处:网络
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\\m

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.


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.


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.


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.


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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号