Path的常用方法

函数列表

对一个路径做相应操作,包括文件路径,目录路径,通常会用到Path这个类, 本文列举一些常用的操作。

获取指定路径字符串的目录信息

public static string GetDirectoryName(string path)

直接看几个示例了:

string fileName = @"C:\mydir\myfile.ext"; 
string path = @"C:\mydir\";
string rootPath = @"C:\";
Path.GetDirectoryName(fileName); //返回:'C:\mydir' 
Path.GetDirectoryName(path); //返回:'C:\mydir' 
Path.GetDirectoryName(rootPath); //返回:''
 

获取指定路径字符串的扩展名

public static string GetExtension(string path)

合并两个字符路径字符串

public static string Combine(string path1,string path2)

更改路径字符串的扩展名

public static string ChangeExtension(string path,string extension)

常用方法二

Path//对文件或目录的路径进行操作(很方便)【字符串】

修改后缀,非重命名文件

string s = Path.ChangeExtension(@"C:\temp\F3.png", "jpg");

组合在一起.c:\temp\a.jpg

string s = Path.Combine(@"c:\temp","a.jpg");

获得文件的路径

Path.GetDirectoryName(@"c:\temp\a.jpg");

其他方法

string GetExtension(string path) 得到文件的扩展名

string GetFileName(string path) 得到文件路径的文件名部分

string GetFileNameWithoutExtension(string path) 得到去除扩展名的文件名

string GetFullPath(string path) 得到文件的全路径。可以根据相对路径获得绝对路径。(比如:路径中含有../../之类的,可以得到绝对路径)

string GetTempFileName()  得到一个唯一的临时文件名(*)

string GetTempPath() 得到临时文件夹的路径(*)

MSDN资料

MSDN:http://msdn.microsoft.com/zh-cn/library/System.IO.Path_methods(v=vs.80).aspx

C# System.IO.Path的更多相关文章

  1. C#、.Net代码精简优化(空操作符(??)、as、string.IsNullOrEmpty() 、 string.IsNullOrWhiteSpace()、string.Equals()、System.IO.Path 的用法)

    一.空操作符(??)在程序中经常会遇到对字符串或是对象判断null的操作,如果为null则给空值或是一个指定的值.通常我们会这样来处理: .string name = value; if (name ...

  2. System.IO.Path文件路径类

    Path类的静态属性和方法,此类操作不影响物料文件. 属性 char a = System.IO.Path.VolumeSeparatorChar;//: char b = System.IO.Pat ...

  3. C#使用System.IO.Path获取文件路径、文件名

    class Program { static void Main(string[] args) { //获取当前运行程序的目录 string fileDir = Environment.Current ...

  4. [原]System.IO.Path.Combine 路径合并

    使用 ILSpy 工具查看了 System.IO.Path 类中的 Combine 方法 对它的功能有点不放心,原方法实现如下: // System.IO.Path /// <summary&g ...

  5. 使用System.IO.Combine(string path1, string path2, string path3)四个参数的重载函数提示`System.IO.Path.Combine(string, string, string, string)' is inaccessible due to its protection level

    今天用Unity5.5.1开发提取Assets目录的模块,使用时采用System.IO.Path.Combine(string, string, string, string)函数进行路径生成 明明是 ...

  6. System.IO.Path类

    System.IO.Path为路径的操作封装了很多很有的东西,利用该类提供的方法能够快速处理路径操作的问题.下面详细了解一下. 1.属性 属性太复杂了,反映什么系统平台的信息,看不懂,等以后看得懂了再 ...

  7. System.IO.Path 文件名、路径、扩展名 处理

    string filePath =@"E:/Randy0528/中文目录/JustTest.rar"; 更改路径字符串的扩展名.System.IO.Path.ChangeExten ...

  8. System.IO.Path

    System.IO.Path 分类: C#2011-03-23 10:54 1073人阅读 评论(0) 收藏 举报 扩展磁盘string2010c System.IO.Path提供了一些处理文件名和路 ...

  9. System.IO.Path 操作

    System.IO.Path 分类: C#2011-03-23 10:54 1073人阅读 评论(0) 收藏 举报 扩展磁盘string2010c System.IO.Path提供了一些处理文件名和路 ...

  10. System.IO.Path 文件名、路径、扩展名处理

    string filePath =@"E:/Randy0528/中文目录/JustTest.rar"; 更改路径字符串的扩展名.System.IO.Path.ChangeExten ...

随机推荐

  1. gateway-workman

    最外层start.php,设置全局启动模式,加载Application里的个子服务目录下应用的启动文件(start开头,这些文件都是workman\work类的子类,在载入文件的同时,这些子服务会生成 ...

  2. Centos7下使用yum安装lnmp zabbix3.2

    1:配置epel-release mysql zabbix 源 配置epel源 wget http://mirrors.aliyun.com/epel/epel-release-latest-7.no ...

  3. FineUI开源版(ASP.Net)初学手册

    女朋友鄙视我原创少... 1.下载 进入官方论坛:http://www.fineui.com/bbs/ 要用到下载源代码和空项目下载 http://fineui.codeplex.com/ http: ...

  4. postman学习笔记(二)

    昨天刚操作了一遍最简单的接口测试,今天就收到了俩json文件,一个是postman里导出的接口列表一个是环境变量.拿到的时候一脸懵逼,昨天还以为学会用postman测试接口了,今天才发现哪儿到哪儿呀. ...

  5. 【angular5项目积累总结】breadcrumb面包屑组件

    view code <div class="fxs-breadcrumb-wrapper" aria-label="Navigation history" ...

  6. [C#]使用RabbitMQ模拟抽奖系统的例子

    背景:在实际的项目中,经常有客户需要做抽奖的活动,大部分的都是注册送产品.送红包这些需求.这都是有直接的利益效果,所以经常会遇见系统被盗刷的情况,每一次遇见这种项目的上线都是绷紧神经,客户又都喜欢在过 ...

  7. Spring源码分析:非懒加载的单例Bean初始化过程(下)

    上文Spring源码分析:非懒加载的单例Bean初始化过程(上),分析了单例的Bean初始化流程,并跟踪代码进入了主流程,看到了Bean是如何被实例化出来的.先贴一下AbstractAutowireC ...

  8. 音频播放时出现 Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. https://goo.gl/xX8pDD

    <audio id="play" controls="controls" loop="loop"> <source src ...

  9. [日常] Go语言圣经-指针对象的方法-bit数组习题2

    练习 6.3: (*IntSet).UnionWith会用|操作符计算两个集合的交集,我们再为IntSet实现另外的几个函数IntersectWith(交集:元素在A集合B集合均出现),Differe ...

  10. Java基础——Servlet(六)分页相关

    前面写了Servlet(一)到(五),主要是在网上搜罗的视频.对分页这块还是不太清楚.于是有找到一些视频,重新学习了一下.主要是对分页的认识和设计思路.也是为了方便我以后回忆一下.. 一.分页常识 p ...