這篇文章主要介紹了C#的path.GetFullPath 獲取上級目錄實現方法,包含了具體的C#實現方法以及ASP.net與ASP等的方法對比,非常具有實用價值,需要的朋友可以參考下
本文實例講述了C#的path.GetFullPath 獲取上級目錄實現方法,分享給大家供大家參考。具體實現方法如下:
string path = new directoryinfo("../").fullname;//當前應用程序路徑的上級目錄
獲取當前目錄可以使用
1 appdomain.currentdomain.basedirectory
獲取當前目錄的上級目錄
1 path.getfullpath("..")
具體代碼如下:
1 using system; 2 using system.collections.generic; 3 using system.linq; 4 using system.text; 5 using system.io; 6 namespace pathtest 7 { 8 class program 9 { 10 static void main(string[] args) 11 { 12 //使用appdomain獲取當前應用程序集的執行目錄 13 string dir = appdomain.currentdomain.basedirectory; 14 string info = string.format("appdomain方法獲取當前程序集目錄:{0}", dir); 15 console.writeline(info); 16 //使用path獲取當前應用程序集的執行的上級目錄 17 dir = path.getfullpath(".."); 18 info = string.format("path方法獲取當前程序集上級目錄:{0}", dir); (www.jb51.net) 19 console.writeline(info); 20 //使用path獲取當前應用程序集的執行目錄的上級的上級目錄 21 dir = path.getfullpath(@"...."); 22 info = string.format("path方法獲取當前程序集目錄的級的上級目錄:{0}", dir); 23 console.writeline(info); 24 //使用path獲取當前應用程序集的執行目錄的上級目錄 25 dir = path.getfullpath(@"......"); 26 info = string.format("path方法獲取當前程序集目錄的上級目錄的上級目錄:{0}", dir); 27 console.writeline(info); 28 //在當前程序集目錄中添加指定目錄 29 dir = path.getfullpath(@"io"); 30 info = string.format("在當前程序集目錄中添加指定目錄:{0}", dir); 31 console.writeline(info); 32 console.read(); 33 } 34 } 35 }
winform比較復雜,我只知道environment.currentdirectory是當前exe的路徑,你要得到上一級的再用這個路徑算。
asp就比.net簡單了,直接../就行了
如果是asp.net直接用server.mappath("~/bg/")就可以了。
希望本文所述對大家的C#程序設計有所幫助。