教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 C# 获取路径中,文件名、目录、扩展名的代码

C# 获取路径中,文件名、目录、扩展名的代码

发布时间:2016-07-12   编辑:jiaochengji.com
分享一例c#取得路径中文件名、目录与扩展名的代码,有需要的朋友参考下。

取得指定路径中的文件名、目录与扩展名,代码:
 

复制代码 代码示例:
string path = "C:\\dir1\\dir2\\foo.txt";
string str = "GetFullPath:" + Path.GetFullPath(path) + "\r\n";
str += "GetDirectoryName:" + Path.GetDirectoryName(path) + "\r\n";
str += "GetFileName:" + Path.GetFileName(path) + "\r\n";
str += "GetFileNameWithoutExtension:" + Path.GetFileNameWithoutExtension(path) + "\r\n";
str += "GetExtension:" + Path.GetExtension(path) + "\r\n";
str += "GetPathRoot:" + 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:\

说明:
path 是如何C# 轻松获取路径中文件名、目录、扩展名等判断目录和文件名的:它把最后一个 \ 后面的内容当作是文件名。

C:\dir1\dir2\foo.txt 文件名是 foo.txt,目录名是 C:\dir1\dir2。
C:\dir1\dir2\ 文件名是零长度字符串,目录名是 C:\dir1\dir2。
C:\dir1\dir2 文件名是 dir2,目录名是 C:\dir1。
C# 轻松获取路径中文件名、目录、扩展名等

您可能感兴趣的文章:
C# 获取路径中,文件名、目录、扩展名的代码
C# Winform中获取文件路径的方法
C#获取当前程序运行路径的方法汇总
C#获取路径的多种方法
有关c#操作目录-文件-字符串的类
php实现抓取不带后缀的图片
asp.net C#获取DLL、程序路径,C#获取桌面、收藏夹等特殊系统路径
PHP抓取远程图片教程(包含不带后缀图片)
c# 获取路径的方法详解
php ftp函数应用(范例,ftp类,创建目录函数等)

关键词: 扩展名  路径  目录   
[关闭]
~ ~