System.IO.Path为路径的操作封装了很多很有的东西,利用该类提供的方法能够快速处理路径操作的问题。下面详细了解一下。

1、属性

  属性太复杂了,反映什么系统平台的信息,看不懂,等以后看得懂了再补充。

2、方法

ChangeExtension          更改路径字符串的扩展名。       

Combine               合并两个路径字符串。       

GetDirectoryName           返回指定路径字符串的目录信息。        

GetExtension            返回指定的路径字符串的扩展名。        

GetFileName              返回指定路径字符串的文件名和扩展名。        

GetFileNameWithoutExtension   返回不具有扩展名的指定路径字符串的文件名。        

GetFullPath              返回指定路径字符串的绝对路径。      

GetInvalidFileNameChars        获取包含不允许在文件名中使用的字符的数组。        

GetInvalidPathChars          获取包含不允许在路径名中使用的字符的数组。        

GetPathRoot              获取指定路径的根目录信息。       

GetRandomFileName          返回随机文件夹名或文件名。        

GetTempFileName          创建磁盘上唯一命名的零字节的临时文件并返回该文件的完整路径。        

GetTempPath            返回当前系统的临时文件夹的路径。     

HasExtension            确定路径是否包括文件扩展名。        

IsPathRooted            获取一个值,该值指示指定的路径字符串是包含绝对路径信息还是包含相对路径信息。

 
            string str = "images/girl.jpg";
string extend = Path.GetExtension(str);
Console.WriteLine(extend); //输出 .jpg string str1 = @"C:\App_Data";
string str2 = @"images\girl.jpg";
string newPath = Path.Combine(str1, str2);
Console.WriteLine(newPath); //输出C:\App_Data\images\girl.jpg //会自动补充反斜杠\ string str = @"C:\girl.jpg";
string newPath = Path.ChangeExtension(str, "gif"); //更改扩展名,但不改变原值
Console.WriteLine(str + " 更改扩展名之后: " + newPath); //newPath为:C:\girl.gif string str = @"C:\App_data\upload\girl.jpg";
string dictory = Path.GetDirectoryName(str);
Console.WriteLine(dictory); //输出 C:\App_data\upload string str = @"C:\App_data\upload\girl.jpg";
string fileName = Path.GetFileName(str);
Console.WriteLine(fileName); //输出 girl.jpg string str = @"C:\App_data\upload\girl.jpg";
string fileNameWithOutEntension = Path.GetFileNameWithoutExtension(str);
Console.WriteLine(fileNameWithOutEntension); //输出 girl string str = @"/upload/girl.jpg";
string fullPath = Path.GetFullPath(str);
Console.WriteLine(fullPath); //输出 C:\upload\girl.jpg 这个东西也可以转换成绝对物理路径 char[] chArr = Path.GetInvalidFileNameChars();
foreach (char c in chArr)
{
Console.Write(c + " "); //输出很多不允许在文件名使用的字符
} char[] chArr = Path.GetInvalidPathChars();
foreach (char c in chArr)
{
Console.Write(c + " "); //输出很多不允许在路径中使用的字符
} string str = @"C:\App_data\upload\girl.jpg";
string rootInfo = Path.GetPathRoot(str);
Console.Write(rootInfo); //输出 C:\ string str = Path.GetRandomFileName();
Console.Write(str); //返回随机文件夹名或文件名 string str = Path.GetTempFileName(); //创建磁盘上唯一命名的零字节的临时文件并返回该文件的完整路径
Console.Write(str); //输出 C:\Users\Administrator\AppData\Local\Temp\temdw85.tmp string str = Path.GetTempPath();
Console.Write(str); //输出 C:\Users\Administrator\AppData\Local\Temp\ string str = @"C:\App_data\upload\girl.jpg";
bool hasExtension = Path.HasExtension(str); //确定路径是否含有扩展名,此处有.jpg返回true
Console.Write(hasExtension); string str = @"C:\App_data\upload\girl.jpg";
bool isPath = Path.IsPathRooted(str); //判断给定的路径是绝对路径还是相对路径,此处返回true如果是App_data\upload\girl.jpg则返回false
Console.Write(isPath); Console.ReadKey();

System.IO.Path类的更多相关文章

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

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

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

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

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

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

  4. 详解C#中System.IO.File类和System.IO.FileInfo类的用法

    System.IO.File类和System.IO.FileInfo类主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间.下面通过程序实例来介绍其主要属性和方法. (1) 文件打开 ...

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

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

  6. System.IO.File类和System.IO.FileInfo类

    1.System.IO.File类 ※文件create, copy,move,SetAttributes,open,exists ※由于File.Create方法默认向所有用户授予对新文件的完全读写. ...

  7. System.IO.Directory类

    1.参考的博客:System.IO.Directory类和System.DirectoryInfo类(http://blog.sina.com.cn/s/blog_614f473101017du4.h ...

  8. 使用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)函数进行路径生成 明明是 ...

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

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

随机推荐

  1. SQL学习笔记三(补充-2)之MySQL数据类型

    阅读目录 一 介绍 二 数值类型 三 日期类型 四 字符串类型 五 枚举类型与集合类型 一 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 ...

  2. usb_submit_urb

    hub_irq() --> usb_submit_urb() usb_start_wait_urb() --> usb_submit_urb() 一旦urb被USB驱动程序正确地创建和初始 ...

  3. POJ 1860 Currency Exchange(最短路&spfa正权回路)题解

    题意:n种钱,m种汇率转换,若ab汇率p,手续费q,则b=(a-q)*p,你有第s种钱v数量,问你能不能通过转化让你的s种钱变多? 思路:因为过程中可能有负权值,用spfa.求是否有正权回路,dis[ ...

  4. 【转载】Java关键字之"transient"

    原文出处:http://blog.csdn.net/lanxuezaipiao/article/details/16358677 transient的作用及使用方法 我们都知道一个对象只要实现了Ser ...

  5. vue项目打包部署到nginx 服务器上

    假如要实现的效果如下 http://ip/vue    =>是进入首页访问的路径是  usr/local/nginx/html/vue http://ip/website     =>是进 ...

  6. c# 如何调用python脚本

    1.net4.5: http://www.jb51.net/article/84418.htm 2.net4.0: https://www.cnblogs.com/shiyingzheng/p/605 ...

  7. 在请求中使用XML Publisher生成文件报错

    在页面上使用按钮生成该文件不报错,但是使用请求就报错. 错误内容如下 Error : No corresponding LOB data found :SELECT L.FILE_DATA FILE_ ...

  8. hdu 3268 09 宁波 现场 I - Columbus’s bargain 读题 最短路 难度:1

    Description On the evening of 3 August 1492, Christopher Columbus departed from Palos de la Frontera ...

  9. jquery插件实现鼠标经过图片右侧显示大图的效果(类似淘宝)

    这个插件的名字elevatezoom,网址为http://www.elevateweb.co.uk/image-zoom,在github上的项目首页为https://github.com/elevat ...

  10. android中自定义view构造函数ContentItemView(Context context, AttributeSet paramAttributeSet)的用处

    自己定义一个view <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...