Visual studio 2010 how to refer to a folder inside a project?

I added a folder with a bunch of images inside my vs2010 project. I want to reference this folder using code. Since the final program can be deployed to another place, the absolute path will not be!

What is the standard way to get relative folder paths?

I'm new to this, so sorry if I ask something too obvious! Thanks.

+4
source share
3 answers

You might want to add images to the resource file so that you can work directly with the image without writing code to search for the file.

In any case, you can get the path to the application:

string appFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); 

And then you can use methods from the Path class, for example Path.Combine to link to a folder.

+2
source
Resources are a good idea. An alternative is that the installation location of the installed application folder is written to the registry when the application is installed. OR do something like
 string dir = System.IO.Path.GetDirectoryName(Application.ExecutablePath); 
+1
source

If these images relate to your executable, then you can either use argv [0] from main () (if it's a command line), or it’s even better to keep track of what MSDN offers:

By convention, argv [0] is the command with which the program is called. However, you can use CreateProcess, and if you use both the first and second arguments (lpApplicationName and lpCommandLine), argv [0] may not be an executable name; use GetModuleFileName to get the name of the executable file and its fully qualified path.

0
source

All Articles