来自于:LST329
Path Class,命名空间:System.IO
Path Class是对包含文件或目录路径信息的 String 实例执行操作。 这些操作是以跨平台的方式执行的。
static void Main(string[] args){string filePath = @"E:\Users\LST\Desktop\test.png";string[] paths = { @"E:\Users", "LST", "Desktop", "Combine.png" };//修改路径的后缀名string str1 = Path.ChangeExtension(filePath, "jpg");//输出效果:E:\Users\LST\Desktop\test.jpgConsole.WriteLine("修改后缀名后的路径:"+str1);//将多个字符串拼接成一个字符串string str2 = Path.Combine(paths);//输出效果:E:\Users\LST\Desktop\Combine.pngConsole.WriteLine("拼接后的路径:" + str2);//获取文件的目录string str3 = Path.GetDirectoryName(filePath);//输出效果:E:\Users\LST\DesktopConsole.WriteLine("文件的目录:" + str3);//获取文件的扩展名string str4 = Path.GetExtension(filePath);//输出效果:pngConsole.WriteLine("文件的扩展名:" + str4);//获取文件的文件名string str5 = Path.GetFileNameWithoutExtension(filePath);//输出效果:testConsole.WriteLine("文件的文件名:" + str5);//获取文件的文件名和扩展名string str6 = Path.GetFileName(filePath);//输出效果:test.pngConsole.WriteLine("文件的文件名和扩展名:" + str6);//获取指定路径的根目录string str7 = Path.GetPathRoot(filePath);//输出效果:E:\Console.WriteLine("路径的根目录:" + str7);//随机生成文件夹名或文件名string str8 = Path.GetRandomFileName();Console.WriteLine("随机生成的名字:" + str8);}
运行后的效果:
