C# 轻松获取路径中文件名、目录、扩展名等

<span style="font-size:18px;">System.IO.Path对包含文件或目录径信息的String实例执行操作。
1、ChangeExtension:更改路径字符串的扩展名
2、Combine(String[]):将字符串数组组合成一个路径
3、Combine(String,String):将两个字符串组合成一个路径
4、Combine(String,String,String):将三个字符串组合成一个路径,四个参数就是将四个字符串组合成一个路径。
5、GetDirectoryName:返回指定字符串的目录信息。
6、GetExtension:获取指定字符串的目录信息。
7、GetFileNameWithoutExtension:返回不具有扩展名的指定路径字符串的文件名。
8、GetFullPath:返回指定路径字符串的绝对路径
9、GetPathRoot:获取指定路径的根目录信息。
10、GetRandomFileName:获取随机文件名或文件夹名</span>

<span style="font-size:24px;">string path = "C:\\dir1\\dir2\\foo.txt";  
string str = "<span style="color:#ff0000;">GetFullPath</span>:" + Path.GetFullPath(path) + "\r\n";  
str += "<span style="color:#ff0000;">GetDirectoryName</span>:" + Path.GetDirectoryName(path) + "\r\n"; 
str += "<span style="color:#ff0000;">GetFileName</span>:" + Path.GetFileName(path) + "\r\n";  
str += "<span style="color:#ff0000;">GetFileNameWithoutExtension</span>:" + Path.GetFileNameWithoutExtension(path) + "\r\n";  
str += "<span style="color:#ff0000;">GetExtension</span>:" + Path.GetExtension(path) + "\r\n"; 
str += "<span style="color:#ff0000;">GetPathRoot</span>:" + Path.GetPathRoot(path) + "\r\n"; 
MessageBox.Show(str); 
结果:  
GetFullPath:C:\dir1\dir2\foo.txt 
GetDirectoryName:C:\dir1\dir2 
GetFileName:foo.txt  
GetFileNameWithoutExtension:foo 
GetExtension:.txt 
GetPathRoot:C:\</span>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值