WebFrom项目(asp.net)下获取路径的写法:
//Web项目从网站根目录获取对应子目录
string path1 = Page.Server.MapPath("~/WebFolder");
//输出:D:\openGPS\Code\Web\WebFolder
string path2 = Page.Server.MapPath("/WebFolder");
//输出:D:\openGPS\Code\Web\WebFolder
//不带~或者/开头,获取的是相对于当前aspx页面的路径
string path3 = Page.Server.MapPath("WebFolder");
//输出:D:\openGPS\Code\Web\Test\WebFolder
string path4 = Page.Server.MapPath("");
//输出:D:\openGPS\Code\Web\Test
WindowsForm项目(exe程序)下获取路径的方法
获取当前路径:
//获取启动应用程序的可执行文件的路径
string path5 = System.Windows.Forms.Application.StartupPath;
//输出:C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW
//获取启动应用程序的可执行文件的路径及文件名
string path6 = System.Windows.Forms.Application.ExecutablePath;
//输出:C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW\vstest.executionengine.x86.exe
通用的输出路径方法
//获取模块的完整路径。
string path7 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//输出:C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 14.0\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW\vstest.executionengine.x86.exe
//获取和设置当前目录(该进程从中启动的目录)的完全限定目录
string path8 = System.Environment.CurrentDirectory;
//输出:D:\openGPS\Code\openGPS.UnitTest\bin\Debug
//获取应用程序的当前工作目录
string path9 = System.IO.Directory.GetCurrentDirectory();
//输出:D:\openGPS\Code\openGPS.UnitTest\bin\Debug
//获取程序的基目录
string path10 = System.AppDomain.CurrentDomain.BaseDirectory;
//输出:D:\openGPS\Code\openGPS.UnitTest\bin\Debug
//获取和设置包括该应用程序的目录的名称
string path11 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//输出:D:\openGPS\Code\openGPS.UnitTest\bin\Debug
原文地址:
https://www.opengps.cn/Blog/View.aspx?id=415
文章的更新编辑依此链接为准。欢迎关注源站原创文章!