C# 项目文件的文件路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12335618/
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
File path for project files?
提问by OscarLeif
I am working on a media player in C# but when I want to make my test I have a problem.
我正在使用 C# 开发媒体播放器,但是当我想进行测试时遇到了问题。
I have to create a new object song with the following path:
我必须使用以下路径创建一首新的对象歌曲:
@"C:\Users\Jesus Antonio\Desktop\JukeboxV2.0\JukeboxV2.0\Datos\ich will.mp3"
It works but when I change the computer I have to rewrite the entire path, My project is called JukeboxV2.0
它可以工作,但是当我更换计算机时,我必须重写整个路径,我的项目称为 JukeboxV2.0
In java I remember you can just write the path for example
在java中我记得你可以写路径例如
@"JukeboxV2.0\JukeboxV2.0\Datos\ich will.mp3"
This will save a lot of time because I can take my project to different computers and it works, but here I don't known how to do that, anyone know?
这将节省大量时间,因为我可以将我的项目带到不同的计算机并且它可以工作,但是在这里我不知道如何做到这一点,有人知道吗?
采纳答案by eandersson
You would do something like this to get the path "Data\ich_will.mp3" inside your application environments folder.
您将执行类似操作以获取应用程序环境文件夹中的路径“Data\ich_will.mp3”。
string fileName = "ich_will.mp3";
string path = Path.Combine(Environment.CurrentDirectory, @"Data\", fileName);
In my case it would return the following:
在我的情况下,它将返回以下内容:
C:\MyProjects\Music\MusicApp\bin\Debug\Data\ich_will.mp3
I use Path.Combineand Environment.CurrentDirectoryin my example. These are very useful and allows you to build a path based on the current location of your application. Path.Combinecombines two or more strings to create a location, and Environment.CurrentDirectoryprovides you with the working directory of your application.
我在我的例子中使用Path.Combine和Environment.CurrentDirectory。这些非常有用,允许您根据应用程序的当前位置构建路径。Path.Combine组合两个或多个字符串以创建一个位置,并Environment.CurrentDirectory为您提供应用程序的工作目录。
The working directory is not necessarily the same path as where your executableis located, but in most cases it should be, unless specified otherwise.
工作目录不一定与您所在的路径相同executable,但在大多数情况下应该是,除非另有说明。
回答by Hyman.li
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"JukeboxV2.0\JukeboxV2.0\Datos\ich will.mp3")
base directory + your filename
基本目录 + 您的文件名
回答by Luiso
I was facing a similar issue, I had a file on my project, and wanted to test a class which had to deal with loading files from the FS and process them some way. What I did was:
我遇到了类似的问题,我的项目中有一个文件,并且想测试一个必须处理从 FS 加载文件并以某种方式处理它们的类。我所做的是:
- added the file
test.txtto my test project - on the solution explorer hit
alt-enter(file properties) - there I set
BuildActiontoContentandCopy to Output DirectorytoCopy if newer, I guessCopy alwayswould have done it as well
- 将文件添加
test.txt到我的测试项目中 - 在解决方案资源管理器命中
alt-enter(文件属性) - 还有我定
BuildAction要Content和Copy to Output Directory来Copy if newer,我想Copy always会做它,以及
then on my tests I just had to Path.Combine(Environment.CurrentDirectory, "test.txt")and that's it. Whenever the project is compiled it will copy the file (and all it's parent path, in case it was in, say, a folder) to the bin\Debug(or whatever configuration you are using) folder.
然后在我的测试中,我只需要这样做就可以Path.Combine(Environment.CurrentDirectory, "test.txt")了。每当编译项目时,它都会将文件(以及它的所有父路径,以防它位于文件夹中)复制到bin\Debug(或您正在使用的任何配置)文件夹中。
Hopes this helps someone
希望这有助于某人

