Tolga Evcimen Tolga Evcimen - 1 year ago 223
C# Question

How to get relative path of a file in visual studio?

I am trying to get the path of an image file which I added in solution explorer in Visual Studio, but I couldn't get the relative path of that image. H is the file structure of my project:


I can get the image like this :


But I should be able to get it with something like :


I guess I don't know the exact syntax of it so I cant fetch the image. :(

Answer Source

With all respect to given other answers, I come up with my own answer after a year, seems like a progress :)

When it is the case that you want to use any kind of external file, it is certainly a way to put them in a folder within your project, but not as valid as getting them from resources. In a regular visual studio project, you should have a Resources.resx file under Properties section, if not you can easily add your own Resource.resx file. And add any kind of file in it, you can reach the walkthrough for adding resource files to your project here.

And after having resource files in your project calling them is easy as this:

var myIcon = Resources.MyIconFile;

And of course you should add the using statement of Properties of your project like this:

using <namespace>.Properties;

I hope this will help someone.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download