C# Path
2021-03-12 02:27
标签:info mp4 new lin 拼接 direct ext 变量 pat C# Path 标签:info mp4 new lin 拼接 direct ext 变量 pat 原文地址:https://www.cnblogs.com/trykle/p/14505546.html变量准备
var filePath = @"c:\doc\1.txt";
var dirPath1 = @"c:\music\";
var dirPath2 = @"c:\movie";
路径拼接
Console.WriteLine(Path.Combine(dirPath1, "a.mp3")); //c:\music\a.mp3
Console.WriteLine(Path.Combine(dirPath2, "b.mp4")); //c:\movie\b.mp4
获取文件夹的名字
Console.WriteLine(Path.GetDirectoryName(dirPath1)); //c:\music
Console.WriteLine(Path.GetDirectoryName(dirPath2)); //c:Console.WriteLine(Path.GetDirectoryName(filePath)); //c:\doc
Console.WriteLine(new DirectoryInfo(dirPath1).Name); //music
Console.WriteLine(new DirectoryInfo(dirPath2).Name); //movie
Console.WriteLine(new DirectoryInfo(@"d:\a\b\c\d").Parent.Name); //c
获取文件名或后缀
Console.WriteLine(Path.GetFileName(filePath)); //1.txt
Console.WriteLine(Path.GetFileNameWithoutExtension(filePath)); //1
Console.WriteLine(Path.GetExtension(filePath)); //.txt