码迷,mamicode.com
首页 > Windows程序 > 详细

C# Path

时间:2021-03-10 12:57:56      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:info   mp4   new   lin   拼接   direct   ext   变量   pat   

变量准备

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 

C# Path

标签:info   mp4   new   lin   拼接   direct   ext   变量   pat   

原文地址:https://www.cnblogs.com/trykle/p/14505546.html

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