67

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

Community
  • 1
  • 1
HuMMeR-SI
  • 791
  • 2
  • 7
  • 10
  • I think this [**link**](http://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx) is what you are looking for. – Mr_Green Jan 27 '13 at 17:03
  • 1
    Do you mean you want the executable path? The project path is only useful for _building_. – Oded Jan 27 '13 at 17:05
  • 7
    I don't think this is a duplicate of the earlier question which was asking for the path to the exe. This one asks for the path to the project! – oefe Jan 27 '13 at 17:20
  • 9
    Not a duplicate for the reason mentioned by @oefe. – Paul T Davies May 19 '14 at 10:09
  • @oefe nominated for reopening. – Kyle Strand Jul 26 '17 at 22:09
  • 2
    @Oded The project path can also be useful for testing. For instance, there may be data files used for tests that are contained in the project but not part of the build, source code, or distributables. – Kyle Strand Jul 26 '17 at 22:10

4 Answers4

100

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, "..\\..\\..\\")) ;
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
takirala
  • 1,891
  • 2
  • 16
  • 36
  • 1
    How to use `..` or `../..` with `System.AppDomain.CurrentDomain.BaseDirectory` ? – Stiger Sep 10 '15 at 23:09
  • 21
    @Stiger: `Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"..\\..\\"))` – Adiono Oct 22 '15 at 23:27
  • 4
    I drop here the .NET Core version, in case someone is looking for that: `Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\\..\\..\\"));` – nopara73 May 12 '17 at 11:56
  • 2
    in vs2017 15.6 this will return the directory of the executable. – DSUK Apr 05 '18 at 16:41
  • For those that don't know, if you have a path, you'll use \\ because in C# you escape a \ with a \. Example: `string path = "C:\\temp\\directory\\somefile.png"` will evaluate to `C:\temp\directory\somefile.png` – Kellen Stuart Jun 28 '19 at 18:17
44

You can use

string wanted_path = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
CharlesB
  • 86,532
  • 28
  • 194
  • 218
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
20
var requiredPath = Path.GetDirectoryName(Path.GetDirectoryName(
System.IO.Path.GetDirectoryName( 
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase )));
nsconnector
  • 838
  • 6
  • 12
10

Your program has no knowledge of where your VS project is, so see get path for my .exe and go ../.. to get your project's path.

Community
  • 1
  • 1
CharlesB
  • 86,532
  • 28
  • 194
  • 218