Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

c# - File path for project files?

I am working on a media player in C# but when I want to make my test I have a problem.

I have to create a new object song with the following path:

@"C:UsersJesus AntonioDesktopJukeboxV2.0JukeboxV2.0Datosich will.mp3"

It works but when I change the computer I have to rewrite the entire path, My project is called JukeboxV2.0

In java I remember you can just write the path for example

@"JukeboxV2.0JukeboxV2.0Datosich 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You would do something like this to get the path "Dataich_will.mp3" inside your application environments folder.

string fileName = "ich_will.mp3";
string path = Path.Combine(Environment.CurrentDirectory, @"Data", fileName);

In my case it would return the following:

C:MyProjectsMusicMusicAppinDebugDataich_will.mp3

I use Path.Combine and Environment.CurrentDirectory in my example. These are very useful and allows you to build a path based on the current location of your application. Path.Combine combines two or more strings to create a location, and Environment.CurrentDirectory provides you with the working directory of your application.

The working directory is not necessarily the same path as where your executable is located, but in most cases it should be, unless specified otherwise.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...