C#IOPath类path.GetFullPath 获取上级目录实现方法

C#的path.GetFullPath 获取上级目录实现方法

这篇文章主要介绍了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#程序设计有所帮助。

原文地址:https://www.cnblogs.com/grj001/p/12225313.html