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#在網(wǎng)絡(luò)波動(dòng)時(shí)防重復(fù)提交的方法
這篇文章主要介紹了淺談C#在網(wǎng)絡(luò)波動(dòng)時(shí)防重復(fù)提交的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04通過(guò)C#實(shí)現(xiàn)發(fā)送自定義的html格式郵件
本篇文章主要介紹了通過(guò)C#實(shí)現(xiàn)發(fā)送自定義的html格式郵件,詳細(xì)的介紹了發(fā)送HTML格式郵件的方法,有興趣的可以了解一下。2017-02-02c# for循環(huán)中創(chuàng)建線程執(zhí)行問(wèn)題
這篇文章主要介紹了有關(guān)c# for循環(huán)中創(chuàng)建線程執(zhí)行問(wèn)題,下面文章將將以舉例的方式展開(kāi)for循環(huán)中創(chuàng)建線程執(zhí)行問(wèn)題的內(nèi)容,需要的朋友可以參考一下,希望對(duì)你有所幫助2021-11-11