string strFullPath = @"d:\test\mytest.txt"; string strFilename = Path.GetFileName(strFullPath);//返回带扩展名的文件名 Trace.WriteLine("返回带扩展名的文件名" + strFilename); string extension = Path.GetExtension(strFullPath);//扩展名 Trace.WriteLine("扩展名" + extension); string strFileNameWithoutExtension = Path.GetFileNameWithoutExtension(strFullPath);// 没有扩展名的文件名 "default" Trace.WriteLine("没有扩展名的文件名" + strFileNameWithoutExtension); string dirPath = Path.GetDirectoryName(strFullPath); //返回文件所在目录 Trace.WriteLine("返回文件所在目录" + dirPath); string strFullPath1 = Path.Combine(@"d:\test", "mytest.txt"); //返回 "d:\test\default.avi" Trace.WriteLine("路径合成" + strFullPath1); string strFullPath2 = Path.GetFullPath("新建文本文档.txt");//返回指定路径字符串的绝对路径 Trace.WriteLine("返回指定路径字符串的绝对路径" + strFullPath2);
运行效果:
返回指定路径字符串的绝对路径C:\Users\Administrator\Desktop\新建文件夹\WindowsFormsApp1\WindowsFormsApp1\bin\Debug\新建文本文档.txt
|