Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File path for project files?

Tags:

c#

.net

path

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:\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

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

@"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?

like image 724
OscarLeif Avatar asked Sep 09 '12 00:09

OscarLeif


People also ask

What is the file path of a file?

A file path describes the location of a file in a web site's folder structure. File paths are used when linking to external files, like: Web pages. Images.

What is path to project?

The key to the project structure is the path used to identify the files in a project—the project path. The project path is used within scripts to describe the location of project resources. This project path is based on the project root and is self-contained, which allows for portability.

How do I set path in project?

In the Project > Files view, you can use the context menu to add or remove folders from the project path. Right-click a folder and select Project Path > Add to Project Path, or Add to the Project Path (Including Subfolders), or one of the options to remove the folder from the path.


2 Answers

You would do something like this to get the path "Data\ich_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:\MyProjects\Music\MusicApp\bin\Debug\Data\ich_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.

like image 131
eandersson Avatar answered Oct 07 '22 00:10

eandersson


Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"JukeboxV2.0\JukeboxV2.0\Datos\ich will.mp3") 

base directory + your filename

like image 24
jack.li Avatar answered Oct 07 '22 01:10

jack.li