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. mailx配置安装

    mailxwget http://ftp.debian.org/debian/pool/main/h/heirloom-mailx/heirloom-mailx_12.5.orig.tar.gztar ...

  2. 如何在官网下载JDK(版本、系统类型、字节位等)

    JDK官网地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 步骤1: 此步注意不要下载运行时jre

  3. scrapy中的canonicalize_url【转】

    转自:http://www.leyle.com/archives/canonicalize_url.html 思考一下:对url进行规范化处理是否是必须的?因为这一步处理涉及到编码转换,对于一个网页的 ...

  4. [原][osgearth]osgearth本地(离线)数据源处理小结

    参考:http://docs.osgearth.org/en/latest/data.html Processing Local Source Data If you have geospatial ...

  5. Mac for MySQL 5.7 安装教程

    一.环境 MAC OS X 10.10 二.下载MySQL 地址:http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.10-osx10.10- ...

  6. jxl将list导入到Excel中供下载

    jxl操作excel /** * 分隔符 */ private final static String SEPARATOR = "|"; /** * 由List导出至指定的Shee ...

  7. 去掉Firefox的标题栏

    Linux Mint里,火狐的标题栏很不美观,不过火狐可是出名的的“可定制”,Hide Caption扩展拖拖地满足你的要求,简直不能再好! 献上链接: https://addons.mozilla. ...

  8. HDU 4522 (恶心建图)

    湫湫系列故事——过年回家 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  9. Leetcode 52

    //N皇后的基础上改了一点class Solution { public: int totalNQueens(int n) { ; vector<); DFS(pos,,res); return ...

  10. Oracle Sourcing Implementation and Administration Guide(转)

    原文地址 Oracle Sourcing Implementation and Administration Guide