路径中获取文件全路径、目录、扩展名、文件名称


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

namespace TBQDLKW
{
    class Program
    {
        static void Main(string[] args)
        {
            //一个文件目录
            string filePath = @"C:\Users\JuYaNan\Desktop\DBCs\ST_CAN.dbc";
            Console.WriteLine("该文件的目录:" + filePath);
            string str = "获取文件的全路径:" + Path.GetFullPath(filePath);
            Console.WriteLine(str);
            str = "获取文件所在的目录:" + Path.GetDirectoryName(filePath);
            Console.WriteLine(str);
            str = "获取文件的名称含有后缀:" + Path.GetFileName(filePath);
            Console.WriteLine(str);
            str = "获取文件的名称没有后缀:" + Path.GetFileNameWithoutExtension(filePath);
            Console.WriteLine(str);
            str = "获取路径的后缀扩展名称:" + Path.GetExtension(filePath);
            Console.WriteLine(str);
            str = "获取路径的根目录:" + Path.GetPathRoot(filePath);
            Console.WriteLine(str);
            Console.ReadKey();
        }
    }
}

结果图:


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM