日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

C#獲取應(yīng)用程序路徑或Web頁(yè)面目錄路徑_C#教程

作者:springsnow ? 更新時(shí)間: 2022-07-08 編程語(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(類名)).CodeBase; //利用反射獲取當(dāng)前程序集的位置
typeof(類名).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ì)路徑。

原文鏈接:https://www.cnblogs.com/springsnow/p/9434002.html

欄目分類
最近更新