简体   繁体   中英

visual studio 2010 how to reference a folder inside a project?

I have added a folder with a bunch of images inside my vs2010 project. I want to reference this folder through code. Since the final prog may be deployed into different location absolute path won't do!

What is the standard way to get the folders relative path?

I am fairly new to this, so sorry if I am asking something too obvious! Thank you.

You might want to add images to the resources file, so you will be able to work directly with an image without writing code to find the file.

Anyway, you can get the application path:

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

And then you can use methods from Path class, like Path.Combine to refer to the folder.

Resources is a good idea. An alternative is to have the Application's installed folder location written to the Registry when the application is installed. OR do something like

string dir = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

If these images are relative to your executable, then you can either use argv[0] from main() (if it's command line), or even better follow what MSDN is advicing:

By convention, argv[0] is the command with which the program is invoked. However, it is possible to spawn a process using CreateProcess and if you use both the first and second arguments (lpApplicationName and lpCommandLine), argv[0] may not be the executable name; use GetModuleFileName to retrieve the executable name, and its fully-qualified path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM