C# MVC 获得程序运行路径 .net core
string filePath = System.Web.HttpContext.Current.Request.MapPath("~/Upload"); //由虚拟路径指定的服务器上的物理路径 string filePath1 = System.Web.HttpContext.Current.Server.MapPath("~/Upload"); //对应于路径的Web服务器上的物理文件路径。 string filePath2 = System.Web.Hosting.HostingEnvironment.MapPath("~/Upload"); //由虚拟路径指定的服务器上的物理路径
string filePath = System.Web.HttpContext.Current.Request.MapPath("");//获得控制器名字目录下的
filePath = System.Web.HttpContext.Current.Request.MapPath("../");//获得控制器的上级目录
winform
var s1 = System.IO.Directory.GetCurrentDirectory(); //D:\abc\bin\Debug
string s2 = System.Windows.Forms.Application.StartupPath; //D:\abc\bin\Debug
string s3 = System.AppDomain.CurrentDomain.BaseDirectory; //D:\abc\Debug\
System.IO.DirectoryInfo topDir = System.IO.Directory.GetParent(System.Environment.CurrentDirectory);
//继续获取上级的上级的上级的目录。
string pathto = topDir.Parent.Parent.FullName;
dynamic type = (new Program()).GetType(); string currentDirectory = System.IO.Path.GetDirectoryName(type.Assembly.Location); //\bin\Debug\netcoreapp2.2 结尾不带斜杠 String basePath1 = AppContext.BaseDirectory; //\bin\Debug\netcoreapp2.2\ // //获得程序的运行路径 string path = AppDomain.CurrentDomain.BaseDirectory; // \bin\Debug\netcoreapp2.2\