c# – How to get relative path of a file in visual studio?
c# – How to get relative path of a file in visual studio?
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;
Im a little late, and Im not sure if this is what youre looking for, but I thought Id add it just in case someone else finds it useful.
Suppose this is your file structure:
/BulutDepoProject
/bin
Main.exe
/FolderIcon
Folder.ico
Main.cs
You need to write your path relative to the Main.exe
file. So, you want to access Folder.ico
, in your Main.cs
you can use:
String path = ..\FolderIcon\Folder.ico
That seemed to work for me!
c# – How to get relative path of a file in visual studio?
Omit the ~:
var path = @FolderIconFolder.ico;
~
doesnt mean anything in terms of the file system. The only place Ive seen that correctly used is in a web app, where ASP.NET replaces the tilde with the absolute path to the root of the application.
You can typically assume the paths are relative to the folder where the EXE is located. Also, make sure that the image is specified as content and copy if newer/copy always in the properties tab in Visual Studio.
Related posts on visual studio :
- How can I disable ReSharper in Visual Studio and enable it again?
- Remove project from visual studio solution
- How can I see all breakpoints in Visual Studio 2013?
- visual studio 2017 – Please select a valid startup item
- Visual Studio Code how to resolve merge conflicts with git?
- c++ – The system cannot find the file specified. in Visual Studio
- visual studio code – Stylelint VScode doesnt work
- Can I run JavaScript in Visual Studio code?
- What is SQL Server Data Tools for Visual Studio?
- How to correctly set PYTHONPATH for Visual Studio Code