一、非Web程序
//基目錄,由程序集沖突解決程序用來探測程序集
1.AppDomain.CurrentDomain.BaseDirectory
//當前工作目錄的完全限定路徑
2.Environment.CurrentDirectory
2.Environment.CurrentDirectory
//當前應用程序的 /bin 目錄的路徑
3.HttpRuntime.BinDirectory
3.HttpRuntime.BinDirectory
二、Web程序
假設Web項目根目錄為:"D:\TestPart\WebDirectoryTest"。
1.HttpContext.Current
//返回與Web服務器上的指定虛擬路徑相對應的物理文件路徑
HttpContext.Current.Server.MapPath("");
2.HttpRuntime
HttpRuntime是為當前應用程序提供一組Asp.Net運行時服務。其中與目錄相關的屬性有以下幾項:
//包含承載在當前應用程序域中的應用程序的目錄的虛擬路徑。
HttpRuntime.AppDomainAppVirtualPath;
結果:"/"
//承載在當前應用程序域中的應用程序的應用程序目錄的物理驅動器路徑
HttpRuntime.AppDomainAppPath;
HttpRuntime.AppDomainAppPath;
結果:"D:\TestPart\WebDirectoryTest\WebDirectoryTest\"
//當前應用程序的 /bin 目錄的路徑
HttpRuntime.BinDirectory;
結果:"D:\TestPart\WebDirectoryTest\WebDirectoryTest\bin\"
//公共語言運行時可執行文件的物理路徑
HttpRuntime.ClrInstallDirectory;
結果:"D:\Windows\Microsoft.NET\Framework\v2.0.50727\"
//應用程序的臨時文件存儲目錄的物理路徑
HttpRuntime.CodegenDie;
結果:"D:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\236b7339\fb0854f2"
//當前應用程序的 Machine.config 文件的物理路徑
HttpRuntime.MachineConfigurationDirectory;
結果:"D:\Windows\Microsoft.NET\Framework\v2.0.50727\Config"
3.HostingEnvironment
//應用程序的不帶尾部反斜杠 (/) 的根虛擬路徑
HostingEnvironment.ApplicationVirtualPath;
HostingEnvironment.ApplicationVirtualPath;
結果:"/"
//磁盤上指向應用程序目錄的物理路徑
HostingEnvironment.ApplicationPhysicalPath;
HostingEnvironment.ApplicationPhysicalPath;
結果:"D:\TestPart\WebDirectoryTest\WebDirectoryTest\"
// 摘要:
// 將虛擬路徑映射到服務器上的物理路徑。
// 參數:
// virtualPath:
// 虛擬路徑(絕對路徑或相對路徑)。
// 返回結果:
// 由 virtualPath 指定的服務器物理路徑。
HostingEnvironment.MapPath("~/Admin");
結果:"D:\TestPart\WebDirectoryTest\WebDirectoryTest\Admin"