问题描述
我正在尝试获取应用程序的根文件夹.我使用了以下代码,但这给出了 bin
文件夹,但我需要的是应用程序的根文件夹.有可能得到这个吗?
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);
推荐答案
您的exe所在的位置是应用程序的根.
The location where your exe is, is the root of the application.
您可以使用 string appPath = Path.GetDirectoryName(Application.ExecutablePath);
来获取应用程序路径.
You can use string appPath = Path.GetDirectoryName(Application.ExecutablePath);
to get the application path.
如果要查找解决方案所在的文件夹,建议从exe位置开始,然后沿目录树移动,直到找到包含.sln文件的文件夹.不太清楚为什么要这么做.
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.
刚发现您正在创建一个asp.net网站.在这种情况下,您应该可以在下面使用(在此处):
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;
}
}
这篇关于如何获取ASP.NET应用程序的根文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!