如何获得在Visual Studio中文件的相对路径? [英] How to get relative path of a file in visual studio?

查看:850
本文介绍了如何获得在Visual Studio中文件的相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我在Solution Explorer中添加在Visual Studio中的图像文件的路径,但我无法得到该图像的相对路径。 H是我的项目的文件结构:

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

我能得到这样的形象:

"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. :(

推荐答案

通过所有对于给定其他的答案,我想出了一年之后我自己的答案,似乎是一个进步:)

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

当它是您要使用的任何一种外部文件的情况下,那肯定是把它们夹在你的项目中的一种方式,但不是为有效从资源获取它们。在常规的Visual Studio项目,你应该在属性 Resources.resx 文件部分,如果不是你可以很容易地添加自己的Resource.resx文件。并添加任何在它的文件,就可以到达演练添加资源文件到您的项目的这里

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;



当然,您应该添加属性你的项目是这样的:

using <namespace>.Properties;



我希望这会帮助别人。

I hope this will help someone.

这篇关于如何获得在Visual Studio中文件的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆