我正在尝试获取应用程序的根文件夹。我使用了以下代码,但这给出了 bin
文件夹,但我需要的是应用程序的根文件夹。有可能得到这个吗?
// 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 所在的位置是应用程序的根目录。
您可以使用 string appPath = Path.GetDirectoryName(Application.ExecutablePath);
获取应用程序路径。
如果您想找到解决方案所在的文件夹,我建议从 exe 位置开始,然后沿着目录树向上移动,直到找到包含 .sln 文件的文件夹。不过不太确定您为什么要这样做。
编辑:刚发现您正在创建一个 asp.net 站点。在这种情况下,您应该能够使用下面的内容(找到 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;
}
}
关于c# - 如何获取 ASP.NET 应用程序的根文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5908343/