Path类的使用

Path类的使用

1.0、原始获取文件名的方法

            int index = fileName.LastIndexOf("\");

           string name = fileName.Substring(index + 1);

           Console.WriteLine(name);

1.4 根据Path类获取文件路径信息

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _12_Path
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string fileName = "D:\\aa\\bb\\cc\\dd\\e.txt";
            //int index = fileName.LastIndexOf("\\");
            //string name = fileName.Substring(index + 1);
            //Console.WriteLine(name);

           
            // 获取文件名称
            Console.WriteLine(Path.GetFileName(fileName));

            // 获取文件夹名称
            Console.WriteLine(Path.GetDirectoryName(fileName));

            // 获取文件全路径
            Console.WriteLine(Path.GetFullPath(fileName));

            // 获取没有后缀的文件名
            Console.WriteLine(Path.GetFileNameWithoutExtension(fileName));

            // 获取文件路径
            Console.WriteLine(Path.GetExtension(fileName));

            Console.ReadKey();

        }
    }
}

1.3 运行结果

posted @ 2022-04-02 15:46  Chris丶Woo  阅读(49)  评论(0编辑  收藏  举报