Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

 /BulutDepoProject     /FolderIcon         Folder.ico         Main.cs 

I can get the image like this :

"C:\\Users\\Tolga\\Desktop\\BulutDepo\\BulutDepoProject\\FolderIcon\\Folder.ico"  

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

"~\\FolderIcon\\Folder.ico" 

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

like image 728
Tolga Evcimen Avatar asked Nov 12 '12 10:11

Tolga Evcimen


People also ask

How do I get the relative path of a file in Visual Studio code?

Press Ctrl+Shift+H (Mac: Cmd+Shift+H ) and start typing the file you want. 🪄 Select your file from the dropdown!

How do you copy a relative path?

In the visual studio code, it has commands to Copy Path and Copy Relative Path (Ctrl+Shift+p->File: Copy Path of Active File).


1 Answers

When it is the case that you want to use any kind of external file, there 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 the 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.

After having resource files in your project, calling them is easy as this:

var myIcon = Resources.MyIconFile; 

Of course you should add the using Properties statement like this:

using <namespace>.Properties; 
like image 146
Tolga Evcimen Avatar answered Oct 14 '22 21:10

Tolga Evcimen