.NET获取当前路径的方法汇总

2019-05-26 01:39:08王振洲

//获取和设置包含该应用程序的目录的名称。
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
result: X:xxxxxx (.exe文件所在的目录+"")

//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
string str = System.Windows.Forms.Application.StartupPath;
result: X:xxxxxx (.exe文件所在的目录)

//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
string str = System.Windows.Forms.Application.ExecutablePath;
result: X:xxxxxxxxx.exe (.exe文件所在的目录+.exe文件名)

//获取应用程序的当前工作目录(不可靠)。
string str = System.IO.Directory.GetCurrentDirectory();
result: X:xxxxxx (.exe文件所在的目录)

.NET中三种获取当前路径的代码

//Web编程  
HttpContext.Current.Server.MapPath("FileName")  
System.Web.HttpContext.Current.Request.Path 

//Windows编程  
System.Environment.CurrentDirectory 

//Mobile编程  
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持易采站长站!