C#的path.GetFullPath 获取上级目录实现方法
本文实例讲述了C#的path.GetFullPath获取上级目录实现方法,分享给大家供大家参考。具体实现方法如下:
stringpath=newdirectoryinfo("../").fullname;//当前应用程序路径的上级目录获取当前目录可以使用
appdomain.currentdomain.basedirectory
获取当前目录的上级目录
path.getfullpath("..")
具体代码如下:
usingsystem;
usingsystem.collections.generic;
usingsystem.linq;
usingsystem.text;
usingsystem.io;
namespacepathtest
{
classprogram
{
staticvoidmain(string[]args)
{
//使用appdomain获取当前应用程序集的执行目录
stringdir=appdomain.currentdomain.basedirectory;
stringinfo=string.format("appdomain方法获取当前程序集目录:{0}",dir);
console.writeline(info);
//使用path获取当前应用程序集的执行的上级目录
dir=path.getfullpath("..");
info=string.format("path方法获取当前程序集上级目录:{0}",dir);(www.nhooo.com)
console.writeline(info);
//使用path获取当前应用程序集的执行目录的上级的上级目录
dir=path.getfullpath(@"....");
info=string.format("path方法获取当前程序集目录的级的上级目录:{0}",dir);
console.writeline(info);
//使用path获取当前应用程序集的执行目录的上级目录
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#程序设计有所帮助。