How to get my project path?

C#FormsPath

C# Problem Overview


> Possible Duplicate:
> get path for my .exe using c#

Hello I have a question: How can I get my root project path? what I mean is the first folder in the project where the solution is. I found that command :

System.IO.Directory.GetCurrentDirectory();

However it gives me a specific path to the release folder: wanted_Path/bin/Release

So is there other code, should I cut it manually or put my files in the Release folder??

C# Solutions


Solution 1 - C#

This gives you the root folder:

System.AppDomain.CurrentDomain.BaseDirectory

You can navigate from here using .. or ./ etc.. , Appending .. takes you to folder where .sln file can be found

For .NET framework (thanks to Adiono comment)

Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"..\\..\\"))

For .NET core here is a way to do it (thanks to nopara73 comment)

Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\\..\\..\\")) ;

Solution 2 - C#

You can use

string wanted_path = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));

Solution 3 - C#

var requiredPath = Path.GetDirectoryName(Path.GetDirectoryName(
System.IO.Path.GetDirectoryName( 
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase )));

Solution 4 - C#

Your program has no knowledge of where your VS project is, so see https://stackoverflow.com/questions/3991933/get-path-for-my-exe-using-c-sharp and go ../.. to get your project's path.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHuMMeR-SIView Question on Stackoverflow
Solution 1 - C#takiralaView Answer on Stackoverflow
Solution 2 - C#Parimal RajView Answer on Stackoverflow
Solution 3 - C#nsconnectorView Answer on Stackoverflow
Solution 4 - C#CharlesBView Answer on Stackoverflow