一、Winform獲取本程序的路徑
1、獲取當前目錄
返回最后不帶“\”的目錄:如D:\Winform\bin\Debug
- System.Windows.Forms.Application.StartupPath;
- System.Environment.CurrentDirectory;
- System.IO.Directory.GetCurrentDirectory();
返回最后帶“\”的目錄(AppDomain應用程序域):如D:\Winform\bin\Debug\
- System.AppDomain.CurrentDomain.BaseDirectory;
- System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
2、獲取當前文件路徑
- System.Windows.Forms.Application.ExecutablePath;
- System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
- System.Reflection.Assembly.GetExecutingAssembly().CodeBase; //或者System.Reflection.Assembly.GetAssembly(typeof(類名)).CodeBase; 利用反射獲取當前程序集的位置
- typeof(類名).Assembly.Location;//利用反射
二、WebForm獲取文件路徑
虛擬目錄名:WebSite1
指向:E:\mis\tools
本網頁:http://localhost/WebSite1/folder/WebForm1.aspx
1、獲取虛擬目錄
根相對路徑:
- System.Web.HttpRuntime.AppDomainAppVirtualPath;
- Request.ApplicationPath ;
根絕對路徑:
- AppDomain.CurrentDomain.BaseDirectory;
- Request.PhsicalApplicaitonPath;
- Server.MapPath(“~”) \\ Server.MapPath("/WebSite1")
2、獲取文件路徑
當前文件相對路徑、絕對路徑
- Request.Path --相對路徑 /WebSite1/folder/WebForm1.aspx
- Request.PhsicalPath --絕對路徑 E:\mis\tools\folder\WebForm1.aspx
- Request.AppRelativeCurrentExecutionFilePath -- ~/folder/WebForm1.aspx
當前目錄
- Server.MapPath(”.”)或Server.MapPath(””); --E:\mis\tools\folder
- Server.MapPath(”./1.jpg”)或Server.MapPath(”1.jpg”); --E:\mis\tools\folder\1.jpg
上一目錄
- Server.MapPath(”..”) -- E:\mis\tools
- Server.MapPath(”../1.jpg”) --(””); --E:\mis\tools\1.jpg 上一目錄下的1.JPG文件
- Server.MapPath(”../..”) --C:\inputpub\wwwroot 上一目錄的上一目錄,到了頂目錄wwwroot
根目錄
- Server.MapPath(”/”) --–C:\inputpub\wwwroot
note:在HTML文件中,用”./”、”../”、”/”表示相對路徑和絕對路徑。