C# 相對路徑


一.C# winform 

1.image 

 pictureBox1.Image = Image.FromFile(Application.StartupPath + @"\1.jpg");

 Application.StartupPath  一般為Debug文件夾下的進行查找

 pictureBox1.Image = Image.FromFile(string path);

string Parth=AppDomain.CurrentDomain.BaseDirectory+@"Photo\empty.png";

AppDomain.CurrentDomain.BaseDirectory 程序運行目錄

 

(轉載)

1、 根目錄

.// 或者直接給出文件名稱,是找根目錄的路徑。

如:path = "test.xml" 與 path = ".//test.xml"是一個意思。

2、 根目錄的上級目錄, 在C#中是指定到bin文件夾里面

..// 應用程序的根目錄的上兩級

3、指定到根目錄上面的三級目錄,也就是C#中與bin同一目錄中

..//..//test.xml 這個意思是在根目錄的三級目錄的gs.mdb文件

如果在這級目錄中你要指定到指定文件夾中的某個文件,應該這樣: ..//..//xml//test.xml意思在這個目錄中的xml文件下面的test.xml文件。

4、指定到根目錄的上四級目錄,如下:

..//..//..// 這樣就指定到根目錄的第四級目錄了。

..//..//..//xml//test.xml 這樣就指定到這個目錄里面的dir這個文件夾里面的gs.mdb這個路徑。

5、某個文件的相對路徑

..//Draw//xml//test.xml這種情況如:例如現在有幾個文件夾xml 、 Draw、 tool,而應用程序現在在xml文件夾里面,那么這個路徑的意思是指定在xml文件的上兩級目錄的Draw的bin中的test.xml的文件路徑。

原文:http://blog.sina.com.cn/s/blog_449f737a0100hj07.html

 

1.獲取和設置當前目錄的完全限定路徑。

string str = System.Environment.CurrentDirectory;

Result: C:/xxx/xxx

2.獲取啟動了應用程序的可執行文件的路徑,不包括可執行文件的名稱。

string str = System. Windows .Forms.Application.StartupPath;

Result: C:/xxx/xxx

3.獲取新的 Process 組件並將其與當前活動的進程關聯的主模塊的完整路徑,包含文件名。

string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

Result: C:/xxx/xxx/xxx.exe

4.獲取當前 Thread 的當前應用程序域的基目錄,它由程序集沖突解決程序用來探測程序集。

string str = System.AppDomain.CurrentDomain.BaseDirectory;

Result: C:/xxx/xxx/

5.獲取應用程序的當前工作目錄。

string str = System.IO.Directory.GetCurrentDirectory();

Result: C:/xxx/xxx

6.獲取和設置包含該應用程序的目錄的名稱。

string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

Result: C:/xxx/xxx/

7.獲取當前進程的完整路徑,包含文件名。

string str = this.GetType().Assembly.Location;

Result: C:/xxx/xxx/xxx.exe

8.獲取啟動了應用程序的可執行文件的路徑,包括可執行文件的名稱。

string str = System. Windows .Forms.Application.ExecutablePath;

Result: C:/xxx/xxx/xxx.exe

此外,更多見的通過XML文件配置具體的路徑來達到合理的規划配置文件的具體存放位置,如WEB中的配置文件中的路徑。

 


string str1 =Process.GetCurrentProcess().MainModule.FileName;//可獲得當前執行的exe的文件名。
string str2=Environment.CurrentDirectory;//獲取和設置當前目錄(即該進程從中啟動的目錄)的完全限定路徑。
//備注 按照定義,如果該進程在本地或網絡驅動器的根目錄中啟動,則此屬性的值為驅動器名稱后跟一個尾部反斜杠(如“C:/”)。如果該進程在子目錄中啟動,則此屬性的值為不帶尾部反斜杠的驅動器和子目錄路徑(如“C:/mySubDirectory”)。
string str3=Directory.GetCurrentDirectory();//獲取應用程序的當前工作目錄。
string str4=AppDomain.CurrentDomain.BaseDirectory;//獲取基目錄,它由程序集沖突解決程序用來探測程序集。
string str5=Application.StartupPath;//獲取啟動了應用程序的可執行文件的路徑,不包括可執行文件的名稱。
string str6=Application.ExecutablePath;//獲取啟動了應用程序的可執行文件的路徑,包括可執行文件的名稱。
string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//獲取或設置包含該應用程序的目錄的名稱。


免責聲明!

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



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