zoukankan      html  css  js  c++  java
  • C#中提取文件路径的目录的各种操作

     1 //获取当前运行程序的目录    
     3   string fileDir = Environment.CurrentDirectory;
     4 
     5  //一个文件目录 
     7   string filePath = "C:\JiYF\BenXH\BenXHCMS.xml"; Console.WriteLine("该文件的目录:"+filePath);
     8 
     9   string str = "获取文件的全路径:" + Path.GetFullPath(filePath);             //-->C:JiYFBenXHBenXHCMS.xml
    10 
    11   str = "获取文件所在的目录:" + Path.GetDirectoryName(filePath);          //-->C:JiYFBenXH Console.WriteLine(str);
    12 
    13   str = "获取文件的名称含有后缀:" + Path.GetFileName(filePath);           //-->BenXHCMS.xml Console.WriteLine(str);
    14 
    15   str = "获取文件的名称没有后缀:" + Path.GetFileNameWithoutExtension(filePath);  //-->BenXHCMS Console.WriteLine(str);
    16 
    17   str = "获取路径的后缀扩展名称:" + Path.GetExtension(filePath);          //-->.xml Console.WriteLine(str);
    18 
    19   str = "获取路径的根目录:" + Path.GetPathRoot(filePath);             //-->C: Console.WriteLine(str); Console.ReadKey();
    20 
    21 
    22 //获取相对路径
    24  /// <summary>         
    26 /// 获取相对路径         
    28 /// </summary>      
    30    /// <returns></returns>       
    32   public string GetFilePath()        
    34   {           
    36     string pathname;           
    38     pathname = Application.StartupPath.ToString();  
    40    Int32 cengshu = 3;    //这个是文件的目录的层数     
    42     for (int i = 0; i < cengshu; i++)          
    44     {               
    46       pathname = pathname.Substring(0, pathname.LastIndexOf('\'));        
    48      }
    49 
    50       return pathname;//相对路径的名字   
    51 
    52    }
  • 相关阅读:
    DateUtil-工具类
    文件上传到服务器
    删除文件以及文件夹
    Git命令<转载 https://www.cnblogs.com/cspku/articles/Git_cmds.html>
    httpd似乎没有在运行,并且正在代理补鞋匠,或者SELinux正在这样做。
    【转载】使用yum安装服务报错:[Errno 14] curl#6
    Cobbler自动化安装部署操作系统
    恢复或重置root密码(Centos7)
    DNS服务器
    Windows Server 2008安装教程
  • 原文地址:https://www.cnblogs.com/MaZai/p/10280510.html
Copyright © 2011-2022 走看看