24.Path类

Path类是做什么的?

对包含文件或目录路径信息的 String 实例执行操作。

方法

namespace Demo {
 
    class Program {

        static void Main(string[] args) {

            string str = @"C:Users22053Desktopa.txt";
            //获得文件名
            Console.WriteLine(Path.GetFileName(str));
            //获得文件名但是不包含扩展名
            Console.WriteLine(Path.GetFileNameWithoutExtension(str));
            //获得文件的扩展名
            Console.WriteLine(Path.GetExtension(str));
            //获得文件所在的文件夹的名称
            Console.WriteLine(Path.GetDirectoryName(str));
            //获得文件所在的全路径
            Console.WriteLine(Path.GetFullPath(str));
            //连接两个字符串作为路径
            Console.WriteLine(Path.Combine(@"c:a", "b.txt"));

            Console.ReadKey();


        }
    }


}

运行结果:

其他方法可参考官方文档

原文地址:https://www.cnblogs.com/lz32158/p/12918250.html