C#的path.GetFullPath 獲取上級目錄實現(xiàn)方法
更新時間:2014年10月28日 10:15:30 投稿:shichen2014
這篇文章主要介紹了C#的path.GetFullPath 獲取上級目錄實現(xiàn)方法,包含了具體的C#實現(xiàn)方法以及ASP.net與ASP等的方法對比,非常具有實用價值,需要的朋友可以參考下
本文實例講述了C#的path.GetFullPath 獲取上級目錄實現(xiàn)方法,分享給大家供大家參考。具體實現(xiàn)方法如下:
復制代碼 代碼如下:
string path = new directoryinfo("../").fullname;//當前應用程序路徑的上級目錄
獲取當前目錄可以使用
復制代碼 代碼如下:
appdomain.currentdomain.basedirectory
獲取當前目錄的上級目錄
復制代碼 代碼如下:
path.getfullpath("..")
具體代碼如下:
復制代碼 代碼如下:
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.io;
namespace pathtest
{
class program
{
static void main(string[] args)
{
//使用appdomain獲取當前應用程序集的執(zhí)行目錄
string dir = appdomain.currentdomain.basedirectory;
string info = string.format("appdomain方法獲取當前程序集目錄:{0}", dir);
console.writeline(info);
//使用path獲取當前應用程序集的執(zhí)行的上級目錄
dir = path.getfullpath("..");
info = string.format("path方法獲取當前程序集上級目錄:{0}", dir); (chabaoo.cn)
console.writeline(info);
//使用path獲取當前應用程序集的執(zhí)行目錄的上級的上級目錄
dir = path.getfullpath(@"....");
info = string.format("path方法獲取當前程序集目錄的級的上級目錄:{0}", dir);
console.writeline(info);
//使用path獲取當前應用程序集的執(zhí)行目錄的上級目錄
dir = path.getfullpath(@"......");
info = string.format("path方法獲取當前程序集目錄的上級目錄的上級目錄:{0}", dir);
console.writeline(info);
//在當前程序集目錄中添加指定目錄
dir = path.getfullpath(@"io");
info = string.format("在當前程序集目錄中添加指定目錄:{0}", dir);
console.writeline(info);
console.read();
}
}
}
using system.collections.generic;
using system.linq;
using system.text;
using system.io;
namespace pathtest
{
class program
{
static void main(string[] args)
{
//使用appdomain獲取當前應用程序集的執(zhí)行目錄
string dir = appdomain.currentdomain.basedirectory;
string info = string.format("appdomain方法獲取當前程序集目錄:{0}", dir);
console.writeline(info);
//使用path獲取當前應用程序集的執(zhí)行的上級目錄
dir = path.getfullpath("..");
info = string.format("path方法獲取當前程序集上級目錄:{0}", dir); (chabaoo.cn)
console.writeline(info);
//使用path獲取當前應用程序集的執(zhí)行目錄的上級的上級目錄
dir = path.getfullpath(@"....");
info = string.format("path方法獲取當前程序集目錄的級的上級目錄:{0}", dir);
console.writeline(info);
//使用path獲取當前應用程序集的執(zhí)行目錄的上級目錄
dir = path.getfullpath(@"......");
info = string.format("path方法獲取當前程序集目錄的上級目錄的上級目錄:{0}", dir);
console.writeline(info);
//在當前程序集目錄中添加指定目錄
dir = path.getfullpath(@"io");
info = string.format("在當前程序集目錄中添加指定目錄:{0}", dir);
console.writeline(info);
console.read();
}
}
}
winform比較復雜,我只知道environment.currentdirectory是當前exe的路徑,你要得到上一級的再用這個路徑算。
asp就比.net簡單了,直接../就行了
如果是asp.net直接用server.mappath("~/bg/")就可以了。
希望本文所述對大家的C#程序設計有所幫助。
您可能感興趣的文章:
相關文章
C#實現(xiàn)讓ListBox適應最大Item寬度的方法
這篇文章主要介紹了C#實現(xiàn)讓ListBox適應最大Item寬度的方法,涉及ListBox控件的操作技巧,需要的朋友可以參考下2015-05-05C# WebApi Get請求方式傳遞實體參數(shù)的方法示例
這篇文章主要給大家介紹了關于C# WebApi Get請求方式傳遞實體參數(shù)的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用C#具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04