SoFunction
Updated on 2025-03-07

C# Get application path or web page directory path

1. Winform obtains the path to this program

1. Get the current directory

Return to the directory with the last "\": such as D:\Winform\bin\Debug

;
;
();

Return to the last directory with "\" (AppDomain application domain): such as D:\Winform\bin\Debug\

;
;

2. Get the current file path

;
().;
().CodeBase; //or(typeof(Class Name)).CodeBase; //Use reflection to get the current assembly locationtypeof(Class Name).;//Utilize reflection

2. WebForm gets the file path

Virtual directory name: WebSite1

Point to: E:\mis\tools

This web page: http://localhost/WebSite1/folder/

1. Obtain the virtual directory

Root relative path:

;
;

Root absolute path:

;
; 
(“~”) \\ ("/WebSite1")

2. Get the file path

The relative path and absolute path of the current file

      //Relative path /WebSite1/folder/      //Absolute path E:\mis\tools\folder\      //~/folder/

Current directory

(”.”)or(””);      //E:\mis\tools\folder
(”./”)or(””);     //E:\mis\tools\folder\

Previous Directory

(”..”)     // E:\mis\tools
(”../”) //(""); //E:\mis\tools\ File in the previous directory(”../..”)     //C:\inputpub\wwwroot Previous directory,Arrive at the top directorywwwroot

Root directory

(”/”)  //C:\inputpub\wwwroot

note: In HTML files, use "./", "../", "/" to represent the relative path and the absolute path.

This is all about this article about obtaining file paths in C#. I hope it will be helpful to everyone's learning and I hope everyone will support me more.