码迷,mamicode.com
首页 > 其他好文 > 详细

Path类对路径字符串的操作

时间:2017-09-10 01:16:54      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:directory   测试结果   字符串的操作   getpath   取出   处理   分享   实现   扩展   

在写程序时,不时会用到处理文件路径的问题,例如:取得扩展名、从路径中取出文件名、路径合并、取出或者去年扩展名等。这些功能都可以通过System.IO.Path类提供的方法来实现。这些相关功能用过多次了,但是由于没有系统的整理,掌握得不够牢固,每次用到时都需要再重新查一遍,或者重新测试一下。今天把我想到的常用功能统一测试了一下,并把测试结果写下来,供我自己以及需要类似功能的人参考。

以下代码是在VSTS中写的单元测试代码。

技术分享
[TestMethod]
public void testPath()
{
string fullPath= @"c:\dir1\dir1.2\abc.txt.exe";
string dir = @"c:\dir1\dir1.2";
string file = "abc.txt.exe";
string ext = ".exe";
string root = "c:\\";
string t;
t = Path.GetDirectoryName(fullPath);
Assert.AreEqual(dir, t);
Assert.IsTrue(!t.EndsWith("\\"));
t = Path.GetFileName(fullPath);
Assert.AreEqual(file, t);
t = Path.GetExtension(file);
Assert.AreEqual(ext, t);
t = Path.GetPathRoot(fullPath);
Assert.AreEqual(root, t);
}
技术分享

Path类对路径字符串的操作

标签:directory   测试结果   字符串的操作   getpath   取出   处理   分享   实现   扩展   

原文地址:http://www.cnblogs.com/tinaluo/p/7499814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!