C#獲取應(yīng)用程序路徑或Web頁(yè)面目錄路徑
一、Winform獲取本程序的路徑
1、獲取當(dāng)前目錄
返回最后不帶“\”的目錄:如D:\Winform\bin\Debug
System.Windows.Forms.Application.StartupPath; System.Environment.CurrentDirectory; System.IO.Directory.GetCurrentDirectory();
返回最后帶“\”的目錄(AppDomain應(yīng)用程序域):如D:\Winform\bin\Debug\
System.AppDomain.CurrentDomain.BaseDirectory; System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
2、獲取當(dāng)前文件路徑
System.Windows.Forms.Application.ExecutablePath; System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; System.Reflection.Assembly.GetExecutingAssembly().CodeBase; //或者 System.Reflection.Assembly.GetAssembly(typeof(類(lèi)名)).CodeBase; //利用反射獲取當(dāng)前程序集的位置 typeof(類(lèi)名).Assembly.Location;//利用反射
二、WebForm獲取文件路徑
虛擬目錄名:WebSite1
指向:E:\mis\tools
本網(wǎng)頁(yè):http://localhost/WebSite1/folder/WebForm1.aspx
1、獲取虛擬目錄
根相對(duì)路徑:
System.Web.HttpRuntime.AppDomainAppVirtualPath; Request.ApplicationPath;
根絕對(duì)路徑:
AppDomain.CurrentDomain.BaseDirectory;
Request.PhsicalApplicaitonPath;
Server.MapPath(“~”) \\ Server.MapPath("/WebSite1")2、獲取文件路徑
當(dāng)前文件相對(duì)路徑、絕對(duì)路徑
Request.Path //相對(duì)路徑 /WebSite1/folder/WebForm1.aspx Request.PhsicalPath //絕對(duì)路徑 E:\mis\tools\folder\WebForm1.aspx Request.AppRelativeCurrentExecutionFilePath //~/folder/WebForm1.aspx
當(dāng)前目錄
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文件中,用”./”、”../”、”/”表示相對(duì)路徑和絕對(duì)路徑。
到此這篇關(guān)于C#獲取文件路徑的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中Kestrel和IIS服務(wù)器下的同步與異步配置
本篇文章主要講解什么是Kestrel和IIS服務(wù)器和特點(diǎn),以及他們?nèi)绾闻渲猛脚c異步,具有一定的參加價(jià)值,感興趣的可以了解一下2023-08-08
c#?如何將字符串轉(zhuǎn)換為大寫(xiě)或小寫(xiě)
這篇文章主要介紹了c#?如何將字符串轉(zhuǎn)換為大寫(xiě)或小寫(xiě),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
unity 如何使用文件流讀取streamingassets下的資源
這篇文章主要介紹了unity 使用文件流讀取streamingassets下的資源操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04

