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);
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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;
}
}
Method 2
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.
Method 3
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
Method 4
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/
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0