一般一个文件的全称由名称加类型构成,那么给我们一个路径,我们怎么单纯地获取所需要的文件名称,或者单纯地获取文件类型名(即后缀名),或者获取全称呢。
例如:路径是D:\test\book.png,怎么获取“book.png”呢?怎么获取“book”呢?怎么获取“png”呢?
方法
路径 :
string fullPath=@"D:\test\book.png";
1、获取带扩展名的文件名——book.png
string fileName=System.IO.Path.GetFileName(fullPath);
2、获取不带扩展名的文件名——book
string fileName=System.IO.Path.GetFileNameWithoutExtension(fullPath);
3、获取扩展名——.png
string fileName=System.IO.Path.GetExtension(fullPath);
4、获取文件所在目录——D:\test
string filePath=System.IO.Path.GetDirectoryName(fullPath);
5、获取文件的全路径——D:\test\book.png
string fullPath=Path.GetFullPath("book.png");