49

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


  • I think this link is what you are looking for. - Mr_Green
  • Do you mean you want the executable path? The project path is only useful for building. - Oded
  • 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
  • Not a duplicate for the reason mentioned by @oefe. - Paul T Davies
  • @oefe nominated for reopening. - Kyle Strand

4 답변


38

You can use

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


  • For me it returns "C:\\Program Files (x86)\\Common Files\\Microsoft Shared" instead of project path - algreat
  • that above code is applicable only when you are running it from within visual studio! - PaRiMaL RaJ
  • Why don't use System.AppDomain.CurrentDomain.BaseDirectory ? It is simpler, easy to read and gives correct path - algreat
  • @algreat - he is askin for root proj path, whch is at 2 lower level! - PaRiMaL RaJ

17

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


73

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


  • How to use .. or ../.. with System.AppDomain.CurrentDomain.BaseDirectory ? - Stiger
  • @Stiger: Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"..\\..\\")) - Adiono
  • I drop here the .NET Core version, in case someone is looking for that: Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\\..\\..\\")); - nopara73
  • in vs2017 15.6 this will return the directory of the executable. - DSUK

6

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.


  • @CharlesB How to use ../.. ? - Stiger
  • Just use it as a relative path, it refers to the project directory - CharlesB

Linked


Related

Latest