Path类对路径字符串的操作

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

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

复制代码
[TestMethod]
public void testPath()
{
string fullPath= @"c:dir1dir1.2abc.txt.exe";
string dir = @"c:dir1dir1.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);
}
复制代码
原文地址:https://www.cnblogs.com/tinaluo/p/7499814.html