zoukankan      html  css  js  c++  java
  • C# 轻松获取路径中文件名、目录、扩展名等

    <span style="font-size:18px;">System.IO.Path对包含文件或目录径信息的String实例执行操作。
    1、ChangeExtension:更改路径字符串的扩展名
    2、Combine(String[]):将字符串数组组合成一个路径
    3、Combine(String,String):将两个字符串组合成一个路径
    4、Combine(String,String,String):将三个字符串组合成一个路径,四个参数就是将四个字符串组合成一个路径。
    5、GetDirectoryName:返回指定字符串的目录信息。
    6、GetExtension:获取指定字符串的目录信息。
    7、GetFileNameWithoutExtension:返回不具有扩展名的指定路径字符串的文件名。
    8、GetFullPath:返回指定路径字符串的绝对路径
    9、GetPathRoot:获取指定路径的根目录信息。
    10、GetRandomFileName:获取随机文件名或文件夹名</span>
    
    <span style="font-size:24px;">string path = "C:\dir1\dir2\foo.txt";  
    string str = "<span style="color:#ff0000;">GetFullPath</span>:" + Path.GetFullPath(path) + "
    ";  
    str += "<span style="color:#ff0000;">GetDirectoryName</span>:" + Path.GetDirectoryName(path) + "
    "; 
    str += "<span style="color:#ff0000;">GetFileName</span>:" + Path.GetFileName(path) + "
    ";  
    str += "<span style="color:#ff0000;">GetFileNameWithoutExtension</span>:" + Path.GetFileNameWithoutExtension(path) + "
    ";  
    str += "<span style="color:#ff0000;">GetExtension</span>:" + Path.GetExtension(path) + "
    "; 
    str += "<span style="color:#ff0000;">GetPathRoot</span>:" + Path.GetPathRoot(path) + "
    "; 
    MessageBox.Show(str); 
    结果:  
    GetFullPath:C:dir1dir2foo.txt 
    GetDirectoryName:C:dir1dir2 
    GetFileName:foo.txt  
    GetFileNameWithoutExtension:foo 
    GetExtension:.txt 
    GetPathRoot:C:</span>

  • 相关阅读:
    江湖盛传“阿里三板斧”,其实这才是全部真相!
    PHP算法之四大基础算法
    PHP实现的毫秒定时器,同时解决进程不重复堆积
    leetcode小题解析
    PHP算法之二分查找
    elastic学习笔记
    php中mysqli 处理查询结果集总结
    PHP中的 Iterator 与 Generator
    Laravel源码解析之反射的使用
    PHP下的异步尝试四:PHP版的Promise
  • 原文地址:https://www.cnblogs.com/dengshiwei/p/4258696.html
Copyright © 2011-2022 走看看