C#獲取應用程序 或Web頁面目錄的路徑


一、Winform獲取本程序的路徑

1、獲取當前目錄

返回最后不帶“\”的目錄:如D:\Winform\bin\Debug

 

  1. System.Windows.Forms.Application.StartupPath;
  2. System.Environment.CurrentDirectory;
  3. System.IO.Directory.GetCurrentDirectory();

 

返回最后帶“\”的目錄(AppDomain應用程序域):如D:\Winform\bin\Debug\

 

  1. System.AppDomain.CurrentDomain.BaseDirectory;
  2. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

 

2、獲取當前文件路徑

 

  1. System.Windows.Forms.Application.ExecutablePath;
  2. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
  3. System.Reflection.Assembly.GetExecutingAssembly().CodeBase; //或者System.Reflection.Assembly.GetAssembly(typeof(類名)).CodeBase; 利用反射獲取當前程序集的位置
  4. typeof(類名).Assembly.Location;//利用反射

 

二、WebForm獲取文件路徑

虛擬目錄名:WebSite1

指向:E:\mis\tools

本網頁:http://localhost/WebSite1/folder/WebForm1.aspx

1、獲取虛擬目錄

根相對路徑:

  1. System.Web.HttpRuntime.AppDomainAppVirtualPath;
  2. Request.ApplicationPath ;

根絕對路徑:

  1. AppDomain.CurrentDomain.BaseDirectory;
  2. Request.PhsicalApplicaitonPath; 
  3. Server.MapPath(“~”) \\ Server.MapPath("/WebSite1")

2、獲取文件路徑

當前文件相對路徑、絕對路徑

  1. Request.Path      --相對路徑 /WebSite1/folder/WebForm1.aspx
  2. Request.PhsicalPath      --絕對路徑 E:\mis\tools\folder\WebForm1.aspx
  3. Request.AppRelativeCurrentExecutionFilePath      -- ~/folder/WebForm1.aspx

當前目錄

  1. Server.MapPath(”.”)或Server.MapPath(””);      --E:\mis\tools\folder
  2. Server.MapPath(”./1.jpg”)或Server.MapPath(”1.jpg”);     --E:\mis\tools\folder\1.jpg

上一目錄

  1. Server.MapPath(”..”)     -- E:\mis\tools
  2. Server.MapPath(”../1.jpg”) --(””);     --E:\mis\tools\1.jpg 上一目錄下的1.JPG文件
  3. Server.MapPath(”../..”)     --C:\inputpub\wwwroot 上一目錄的上一目錄,到了頂目錄wwwroot

根目錄

  1. Server.MapPath(”/”) --–C:\inputpub\wwwroot

note:在HTML文件中,用”./”、”../”、”/”表示相對路徑和絕對路徑。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM