C# 如何在visual studio中获取文件的相对路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13342123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get relative path of a file in visual studio?
提问by Tolga Evcimen
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:
我正在尝试获取在 Visual Studio 的解决方案资源管理器中添加的图像文件的路径,但无法获取该图像的相对路径。H是我项目的文件结构:
/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. :(
我想我不知道它的确切语法,所以我无法获取图像。:(
采纳答案by Tolga Evcimen
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.resxfile under the Propertiessection, if not, you can easily add your own Resource.resxfile. And add any kind of file in it, you can reach the walkthrough for adding resource files to your project here.
当您想使用任何类型的外部文件时,当然有一种方法可以将它们放在项目中的文件夹中,但不如从资源中获取它们有效。在常规的 Visual Studio 项目中,您应该Resources.resx在该Properties部分下有一个文件,如果没有,您可以轻松添加自己的Resource.resx文件。并在其中添加任何类型的文件,您可以在此处访问将资源文件添加到您的项目的演练。
After having resource files in your project, calling them is easy as this:
在您的项目中有资源文件后,调用它们很容易,如下所示:
var myIcon = Resources.MyIconFile;
Of course you should add the using Propertiesstatement like this:
当然你应该添加这样的using Properties语句:
using <namespace>.Properties;
回答by 3Dave
Omit the "~\":
省略“~\”:
var path = @"FolderIcon\Folder.ico";
~\doesn't mean anything in terms of the file system. The only place I've 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.
~\在文件系统方面没有任何意义。我见过的唯一正确使用的地方是在 Web 应用程序中,其中 ASP.NET 将波浪号替换为应用程序根目录的绝对路径。
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.
您通常可以假设路径是相对于 EXE 所在文件夹的。此外,请确保在 Visual Studio 的属性选项卡中将图像指定为“内容”和“如果更新则复制”/“始终复制”。
回答by GeorgeT
I also met the same problem and I was able to get it through. So let me explain the steps I applied. I shall explain it according to your scenario.
我也遇到了同样的问题,我能够解决它。那么让我解释一下我应用的步骤。我会根据你的情况来解释。
According to my method we need to use 'Path' class and 'Assembly' class in order to get the relative path.
根据我的方法,我们需要使用 'Path' 类和 'Assembly' 类来获取相对路径。
So first Import System.IO and System.Reflection in using statements.
所以首先在 using 语句中导入 System.IO 和 System.Reflection。
Then type the below given code line.
然后键入下面给定的代码行。
var outPutDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly(). CodeBase);
Actually above given line stores the path of the output directory of your project.(Here 'output' directory refers to the Debugfolder of your project).
实际上上面给定的行存储了您项目的输出目录的路径。(这里的“输出”目录是指您的项目的Debug文件夹)。
Now copy your FolderIcon directory in to the Debug folder. Then type the below given Line.
现在将您的 FolderIcon 目录复制到 Debug 文件夹中。然后键入下面给定的行。
var iconPath = Path.Combine(outPutDirectory, "FolderIcon\Folder.ico");
Now this 'iconPath ' variable contains the entire path of your Folder.ico. All you have to do is store it in a string variable. Use the line of code below for that.
现在这个 'iconPath' 变量包含你的 Folder.ico 的整个路径。您所要做的就是将其存储在字符串变量中。使用下面的代码行。
string icon_path = new Uri(iconPath ).LocalPath;
Now you can use this icon_path string variable as your relative path to the icon.
现在您可以使用这个 icon_path 字符串变量作为图标的相对路径。
Thanks.
谢谢。
回答by Photonic
I think using this will be the easiest
我认为使用这将是最简单的
new Uri("pack://application:,,/FolderIcon/" + youImageICO);
or this code will work on any machine that if your folder is in your root project if you want to change it... just change this section @"..\"
或者这段代码可以在任何机器上运行,如果你的文件夹在你的根项目中,如果你想改变它......只需更改此部分@“..\”
public static string bingPathToAppDir(string localPath)
{
string currentDir = Environment.CurrentDirectory;
DirectoryInfo directory = new DirectoryInfo(
Path.GetFullPath(Path.Combine(currentDir, @"..\..\" + localPath)));
return directory.ToString();
}
回答by AdamInTheOculus
I'm a little late, and I'm not sure if this is what you're looking for, but I thought I'd 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.exefile. So, you want to access Folder.ico, in your Main.csyou can use:
您需要编写相对于Main.exe文件的路径。所以,你想访问Folder.ico,Main.cs你可以使用:
String path = "..\FolderIcon\Folder.ico"
That seemed to work for me!
这似乎对我有用!
回答by Marcin
In Visual Studio please click 'Folder.ico' file in the Solution Explorer pane. Then you will see Properties pane. Change 'Copy to Output Directory' behavior to 'Copy if newer'. This will make Visual Studio copy the file to the output bin directory.
在 Visual Studio 中,请单击“解决方案资源管理器”窗格中的“Folder.ico”文件。然后您将看到“属性”窗格。将“复制到输出目录”行为更改为“如果更新则复制”。这将使 Visual Studio 将文件复制到输出 bin 目录。
Now to get the file path using relative path just type:
现在要使用相对路径获取文件路径,只需键入:
string pathToIcoFile = AppDomain.CurrentDomain.BaseDirectory + "//FolderIcon//Folder.ico";
Hope that helped.
希望有所帮助。

