2

I am trying to get the root folder of the application. I have used the following code, but this gives the bin folder, but what I need is the root folder of the application. Is it possible to get this?

// This is the full directory and exe name
String fullAppName = Assembly.GetExecutingAssembly().GetName().CodeBase;

// This strips off the exe name
String fullAppPath = Path.GetDirectoryName(fullAppName);
Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
user517406
  • 13,623
  • 29
  • 80
  • 120

4 Answers4

11

The location where your exe is, is the root of the application.

You can use string appPath = Path.GetDirectoryName(Application.ExecutablePath); to get the application path.

If you want to find the folder the solution is in, i suggest starting at the exe location, then walking up the directory tree until you get to a folder containing a .sln file. Not too sure why you'd like to do this though.

EDIT: Just figured out you're creating an asp.net site. In which case you should be able to use below (found here):

public static string MappedApplicationPath
{
   get
   {
      string APP_PATH = System.Web.HttpContext.Current.Request.ApplicationPath.ToLower();
      if(APP_PATH == "/")      //a site
         APP_PATH = "/";
      else if(!APP_PATH.EndsWith(@"/")) //a virtual
         APP_PATH += @"/";

      string it = System.Web.HttpContext.Current.Server.MapPath(APP_PATH);
      if(!it.EndsWith(@"\"))
         it += @"\";
      return it;
   }
}
George Duckett
  • 31,770
  • 9
  • 95
  • 162
  • I want the location of the Solution file, because I need to add a folder to this location to save uploaded files in – user517406 May 06 '11 at 08:11
  • 1
    I'd say it might make more sense to have a folder within the folder that the exe is in, not the sln file. However, if the exe is ran from a sub-folder of the one the sln file is in then my suggestion above should work. – George Duckett May 06 '11 at 08:14
  • Is Application.ExecutablePath a new thing in C# 4, as it is not being recognised in VS2008? – user517406 May 06 '11 at 08:23
  • OK, it's Windows Forms rather than Web Forms – user517406 May 06 '11 at 08:31
  • Since you're creating a website, i've re-tagged your post to include asp.net – George Duckett May 06 '11 at 08:36
  • 1
    In case anyone finds this via google, it seems to me this code is way agressive. `System.Web.HttpContext.Current.Server.MapPath(null);` should do. From docs: "If path is null, then the MapPath method returns the full physical path of the directory that contains the current application." – conartist6 Dec 17 '12 at 20:15
  • Or `AppDomain.CurrentDomain.BaseDirectory` - works for Console and ASP.Net. – Alexei Levenkov Aug 20 '15 at 02:43
1

Your program has no knowledge or link to its development environment, so there's no other way for it to know the solution directory other than you to tell him.

Either go to the parent-parente directory, as other have suggested, or pass it as an argument in the executable, or check this SO post (How do you get the solution directory in C#) that have neat ways to do this.

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

what is the 'root' folder if not the folder where the exe file is executed from ?
it could be anything !
you might want to use Directory.Parent to navigate up the tree, but you have to write the code for that specifically

Menahem
  • 3,974
  • 1
  • 28
  • 43
0

If you are trying to modify the solution folder, it may make more sense to create a build script than a runtime application. From those scripts, you have easy access to the solution structure.

http://visualstudiohacks.com/general/customize-your-project-build-process/

Tim M.
  • 53,671
  • 14
  • 120
  • 163