首页 > Windows开发 > 详细

C# 获取文件名、目录、后缀、无后缀文件名、扩展名、根目录等

时间:2018-06-20 16:19:08      阅读:240      评论:0      收藏:0      [点我收藏+]
[csharp] view plain copy
class Program  
    {  
        static void Main(string[] args)  
        {  
  
            //获取当前运行程序的目录  
            string fileDir = Environment.CurrentDirectory;  
            Console.WriteLine("当前程序目录:"+fileDir);  
              
            //一个文件目录  
            string filePath = "C:\\JiYF\\BenXH\\BenXHCMS.xml";  
            Console.WriteLine("该文件的目录:"+filePath);  
  
            string str = "获取文件的全路径:" + Path.GetFullPath(filePath);   //-->C:\JiYF\BenXH\BenXHCMS.xml  
            Console.WriteLine(str);  
            str = "获取文件所在的目录:" + Path.GetDirectoryName(filePath); //-->C:\JiYF\BenXH  
            Console.WriteLine(str);  
            str = "获取文件的名称含有后缀:" + Path.GetFileName(filePath);  //-->BenXHCMS.xml  
            Console.WriteLine(str);  
            str = "获取文件的名称没有后缀:" + Path.GetFileNameWithoutExtension(filePath); //-->BenXHCMS  
            Console.WriteLine(str);  
            str = "获取路径的后缀扩展名称:" + Path.GetExtension(filePath); //-->.xml  
            Console.WriteLine(str);  
            str = "获取路径的根目录:" + Path.GetPathRoot(filePath); //-->C:\  
            Console.WriteLine(str);  
            Console.ReadKey();  
  
        }  
    }  

 

C# 获取文件名、目录、后缀、无后缀文件名、扩展名、根目录等

原文:https://www.cnblogs.com/macT/p/9204246.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!