当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET教程:绝对路径与相对路径的拼合方法

ASP.NET
动态加载Js代码到Head标签中的脚本
asp.net Parameters.AddWithValue方法在SQL语句的 Where 字句中的用法
ASP.NET 运行时错误: 没有为扩展名“.asax”注册的生成提供程序修正版
Convert.ToInt32与Int32.Parse区别及Int32.TryParse
WebService出现"因 URL 意外地以 结束,请求格式无法识别"的解决方法
asp.net(C#) Xml操作(增删改查)练习
asp.net 分页sql语句(结合aspnetpager)
asp.net开发与web标准的冲突问题的一些常见解决方法
asp.net Repeater中使用if的代码
Asp.net FCKEditor 2.6.3 上传文件没有权限解决方法
C# 命名规则(挺不错的)
asp.net 动态生成控件并获取其值
使用DataGrid中扩展ItemRenderer和HeaderRenderer进行操作
asp.net Hashtable 遍历写法
asp.net GridView和DataList实现鼠标移到行行变色
C# 邮件地址是否合法的验证
.net发送邮件实现代码
ASP.Net 上传图片并生成高清晰缩略图
asp.net 事件与委托分析
C# 无限级分类的实现

ASP.NET教程:绝对路径与相对路径的拼合方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-04   浏览: 230 ::
收藏到网摘: n/a

引言

photo在做文件路径处理时,经常需要对一个路径的相对路径进行操作,那么如何拼合相对路径以生成新的绝对路径呢?

 

Path.Combine()方法

我们知道System.IO.Path是专门用来处理路径的静态类,它有一个Combine()方法就是用于拼接路径的,我们来测试一下其拼接效果。

我们使用一个命令行程序进行测试,这里要测试相对于文件C:abc123avatar.html的一系列相对路径,测试代码如下:

class Program

{

static string path = @"C:abc123avatar.html";

 

static void Main(string[] args)

{

Console.WriteLine(path);

Console.WriteLine("输入相对路径以完成合并:");

Console.WriteLine();

while (true)

{

Console.WriteLine("合并为:"+合并路径(Console.ReadLine()));

Console.WriteLine();

}

}

 

private static string 合并路径(string p)

{

return Path.Combine(Path.GetDirectoryName(path), p);

}

}

其中“合并路径”方法的功能是先获取文件的所在目录,再与相对路径拼合。

测试结果:

photo

可以看到,常规的路径拼合没有问题,但是输入“..”就没有被正确处理为上级目录,而是直接进行了合并,这不是我期望看到的。

怎样做才能支持“..”形式的相对路径呢?

 

利用Uri对象的构造函数

我发现Uri对象在构造时可以传入一个基于的Uri及一个相对路径以构造为新的Uri,而我们可以以“file://……”的形式来表示本地文件路径,让我们改动一下代码,进行一下相对Uri的拼合测试。

改动后的代码:

class Program

{

//static string path = @"C:abc123avatar.html";

static string path = @"file:///C:/abc/123/avatar.html";

 

static void Main(string[] args)

{

Console.WriteLine(path);

Console.WriteLine("输入相对路径以完成合并:");

Console.WriteLine();

while (true)

{

//Console.WriteLine("合并为:" + 合并路径(Console.ReadLine()));

Console.WriteLine("合并为:" + 合并Uri(Console.ReadLine()));

Console.WriteLine();

}

}

 

private static string 合并路径(string p)

{

return Path.Combine(Path.GetDirectoryName(path), p);

}

 

private static string 合并Uri(string p)

{

return new Uri(new Uri(path), p).AbsoluteUri;

}

}

测试结果:

photo

好极了,完美支持“../”形式的相对路径!

 

完善

那么接下来的工作就是将路径转换为Uri形式,然后拼合相对路径,再转换回路径形式就可以了。

转换的时候仅仅是采取字符串处理的方法,改动后的代码如下:

class Program

{

static string path = @"C:abc123avatar.html";

 

static void Main(string[] args)

{

Console.WriteLine(path);

Console.WriteLine("输入相对路径以完成合并:");

Console.WriteLine();

while (true)

{

Console.WriteLine("合并为:" + 合并路径(Console.ReadLine()));

Console.WriteLine();

}

}

 

private static string 合并路径(string p)

{

return new Uri(new Uri("file:///" + path.Replace("\", "/")), p.Replace("\", "/")).AbsoluteUri.Substring(8).Replace("/", "\");

}

}

测试结果:

photo

 

 

结语

结果很令人满意,但我总觉得这是个土方子、山寨办法,谁有更简便、正统点的方法吗?

感谢天方这么快就提出了正统的写法:Path.GetFullPath(Path.Combin(@"C:ac","...text"));

我之前找了那么久,又折腾那么久,才弄出个山寨的来,实在汗颜啊,呵呵。