Path 类

  对包含文件或目录路径信息的 String 实例执行操作。

  1.Path.GetExtension 方法 —— 返回指定的路径字符串的扩展名。

  public static string GetExtension(string path)

string fileName = @"C:\mydir.old\myfile.ext";
string path = @"C:\mydir.old\";
string extension; extension = Path.GetExtension(fileName);
Console.WriteLine("GetExtension('{0}') returns '{1}'",
fileName, extension); extension = Path.GetExtension(path);
Console.WriteLine("GetExtension('{0}') returns '{1}'",
path, extension); // This code produces output similar to the following:
//
// GetExtension('C:\mydir.old\myfile.ext') returns '.ext'
// GetExtension('C:\mydir.old\') returns ''

  2.Path.GetFileName 方法 —— 返回指定路径字符串的文件名和扩展名。

  public static string GetFileName(string path)

string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result; result = Path.GetFileName(fileName);
Console.WriteLine("GetFileName('{0}') returns '{1}'",
fileName, result); result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'",
path, result); // This code produces output similar to the following:
//
// GetFileName('C:\mydir\myfile.ext') returns 'myfile.ext'
// GetFileName('C:\mydir\') returns ''

  3.Path.GetFullPath 方法 —— 返回指定相对路径字符串的绝对路径.

    public static string GetFullPath(string path)

string fileName = "myfile.ext";
string path1 = @"mydir";
string path2 = @"\mydir";
string fullPath; fullPath = Path.GetFullPath(path1);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
path1, fullPath); fullPath = Path.GetFullPath(fileName);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
fileName, fullPath); fullPath = Path.GetFullPath(path2);
Console.WriteLine("GetFullPath('{0}') returns '{1}'",
path2, fullPath); // Output is based on your current directory, except
// in the last case, where it is based on the root drive
// GetFullPath('mydir') returns 'C:\temp\Demo\mydir'
// GetFullPath('myfile.ext') returns 'C:\temp\Demo\myfile.ext'
// GetFullPath('\mydir') returns 'C:\mydir'

  4.1 Path.GetTempPath 方法 —— 返回当前用户的临时文件夹的路径。

    string tempPath = Path.GetTempPath();

  4.2 Path.GetRandomFileName 方法 —— 返回随机文件夹名或文件名。

    public static string GetRandomFileName();

  4.3 Path.GetDirectoryName 方法 —— 返回指定路径字符串的目录信息。

    public static string GetDirectoryName(string path)

    

string filePath = @"C:\MyDir\MySubDir\myfile.ext";
string directoryName;
int i = ; while (filePath != null)
{
directoryName = Path.GetDirectoryName(filePath);
Console.WriteLine("GetDirectoryName('{0}') returns '{1}'",
filePath, directoryName);
filePath = directoryName;
if (i == )
{
filePath = directoryName + @"\"; // this will preserve the previous path
}
i++;
}
/*
This code produces the following output: GetDirectoryName('C:\MyDir\MySubDir\myfile.ext') returns 'C:\MyDir\MySubDir'
GetDirectoryName('C:\MyDir\MySubDir') returns 'C:\MyDir'
GetDirectoryName('C:\MyDir\') returns 'C:\MyDir'
GetDirectoryName('C:\MyDir') returns 'C:\'
GetDirectoryName('C:\') returns ''
*/

  5.Path.ChangeExtension 方法 —— 更改路径字符串的扩展名。

  public static string ChangeExtension(string path,string extension)

using System;
using System.IO; public class PathSnippets
{ public void ChangeExtension()
{
string goodFileName = @"C:\mydir\myfile.com.extension";
string badFileName = @"C:\mydir\";
string result; result = Path.ChangeExtension(goodFileName, ".old");
Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'",
goodFileName, result); result = Path.ChangeExtension(goodFileName, "");
Console.WriteLine("ChangeExtension({0}, '') returns '{1}'",
goodFileName, result); result = Path.ChangeExtension(badFileName, ".old");
Console.WriteLine("ChangeExtension({0}, '.old') returns '{1}'",
badFileName, result); // This code produces output similar to the following:
//
// ChangeExtension(C:\mydir\myfile.com.extension, '.old') returns 'C:\mydir\myfile.com.old'
// ChangeExtension(C:\mydir\myfile.com.extension, '') returns 'C:\mydir\myfile.com.'
// ChangeExtension(C:\mydir\, '.old') returns 'C:\mydir\.old'

  6. Path.Combine 方法 —— 将2-4个字符串组合成一个路径。

  public static string Combine(string path1,string path2,*string path3,*string path2)

using System;
using System.IO; public class ChangeExtensionTest { public static void Main() { string path1 = "c:\\temp";
string path2 = "subdir\\file.txt";
string path3 = "c:\\temp.txt";
string path4 = "c:^*&)(_=@#'\\^&#2.*(.txt";
string path5 = "";
string path6 = null; CombinePaths(path1, path2);
CombinePaths(path1, path3);
CombinePaths(path3, path2);
CombinePaths(path4, path2);
CombinePaths(path5, path2);
CombinePaths(path6, path2);
} private static void CombinePaths(string p1, string p2) { try {
string combination = Path.Combine(p1, p2); Console.WriteLine("When you combine '{0}' and '{1}', the result is: {2}'{3}'",
p1, p2, Environment.NewLine, combination);
} catch (Exception e) {
if (p1 == null)
p1 = "null";
if (p2 == null)
p2 = "null";
Console.WriteLine("You cannot combine '{0}' and '{1}' because: {2}{3}",
p1, p2, Environment.NewLine, e.Message);
} Console.WriteLine();
}
}
// This code produces output similar to the following:
//
// When you combine 'c:\temp' and 'subdir\file.txt', the result is:
// 'c:\temp\subdir\file.txt'
//
// When you combine 'c:\temp' and 'c:\temp.txt', the result is:
// 'c:\temp.txt'
//
// When you combine 'c:\temp.txt' and 'subdir\file.txt', the result is:
// 'c:\temp.txt\subdir\file.txt'
//
// When you combine 'c:^*&)(_=@#'\^&#2.*(.txt' and 'subdir\file.txt', the result is:
// 'c:^*&)(_=@#'\^&#2.*(.txt\subdir\file.txt'
//
// When you combine '' and 'subdir\file.txt', the result is:
// 'subdir\file.txt'
//
// You cannot combine '' and 'subdir\file.txt' because:
// Value cannot be null.
// Parameter name: path1

  

C# Path类常用方法的更多相关文章

  1. C# IO操作(一)Path类的常用方法

    1.Path类,查看编译器可知,这个类是个静态的工具类,需要注意的是,这个类是对字符串的操作,与文件无关. 1)ChangeExtension()方法,修改文件的后缀(调用这个方法,如果给第二个参数制 ...

  2. java基础-System类常用方法介绍

    java基础-System类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.System类概念 在API中system类介绍的比较简单,我们给出定义,system中 ...

  3. 04.Path类的学习

    path 是路径的意思. path类是一个静态类,所以path是一个工具类. Path类是专门用来操作路径的. Path的常用方法: namespace _15.Path类的学习 { class Pr ...

  4. 使用File类、StreamRead和StreamWrite读写数据、以及Path类操作文件路径和Directory

    1.File类的概念: File类,是一个静态类,主要是来提供一些函数库用的.静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和 打开一个文件. File类方法的参量 ...

  5. Android中Path类的lineTo方法和quadTo方法画线的区别

    转载:http://blog.csdn.net/stevenhu_223/article/details/9229337 当我们需要在屏幕上形成画线时,Path类的应用是必不可少的,而Path类的li ...

  6. .net学习之集合、foreach原理、Hashtable、Path类、File类、Directory类、文件流FileStream类、压缩流GZipStream、拷贝大文件、序列化和反序列化

    1.集合(1)ArrayList内部存储数据的是一个object数组,创建这个类的对象的时候,这个对象里的数组的长度为0(2)调用Add方法加元素的时候,如果第一次增加元神,就会将数组的长度变为4往里 ...

  7. Android -- 自定义View小Demo,关于Path类的使用(一)

    1,在我们知道自定义view中onDraw()方法是用于绘制图形的,而Path类则是其中的一个重要的类,如下图效果: 代码也没有什么难度,直接贴出来吧 @Override protected void ...

  8. Java 7 中 NIO.2 的使用——第一节 Path 类的使用

    路径隶属于文件系统,实际上它是存储和组织媒体文件的格式,通常在一块或多块硬盘设备上,以便于非常容易地检索.文件系统可以通过  java.nio.file.FileSystems 这个final 类来访 ...

  9. 文件夹和文件、Path类、流、序列化

    循环访问目录树 参考: http://msdn.microsoft.com/zh-cn/library/bb513869.aspx 循环访问目录树”的意思是在指定的根文件夹下,访问每个嵌套子目录中任意 ...

随机推荐

  1. layui与layer同时引入冲突的问题

    之前在项目中只有用layer,但是后来有用到了layui,结果发现同时引入这两个东东 会出现冲突的问题 导致代码运行不正常 后来网上找到了解决办法: 学习源头:http://fly.layui.com ...

  2. Linux Skills

    ++实现RedHat非正常关机的自动磁盘修复先登录到服务器,然后在/etc/sysconfig里增加一个文件autofsck,内容如下:AUTOFSCK_DEF_CHECK=yesPROMPT=yes ...

  3. .Net上传文件超过了最大请求长度

    错误消息:超过了最大请求长度    错误原因:asp.net默认最大上传文件大小为4M,运行超时时间为90S. 修改web.config文件可以改变这个默认值为 <configuration&g ...

  4. 使用YCSB测试mongodb

    项目里面需要对mongodb的性能进行测试,看了下网上很多做法都是使用YCSB进行测试,因此开始学习使用YCSB. 参考资料: YCSB github地址:https://github.com/bri ...

  5. PL/SQL 训练13--plsql 优化

    --数据缓存技术 --PGA和SGA---SGA:系统全局区域--PGA:Process Global Area是为每个连接到Oracle的用户进程保留的内存. ---PLSQL从PGA获取信息的速度 ...

  6. 关于 NULL的坑

    有如下的表: select * from testtable where name in ('name'):  结果是第一条: select * from testtable where name n ...

  7. 主流ETL工具

    主流ETL产品: Ascential公司的Datastage(Datastage在2005年被IBM收购).Informatica公司的Powercenter. NCR Teradata公司的ETL ...

  8. springboot成神之——spring的文件上传

    本文介绍spring的文件上传 目录结构 配置application DemoApplication WebConfig TestController 前端上传 本文介绍spring的文件上传 目录结 ...

  9. PCA主成分分析 ICA独立成分分析 LDA线性判别分析 SVD性质

    机器学习(8) -- 降维 核心思想:将数据沿方差最大方向投影,数据更易于区分 简而言之:PCA算法其表现形式是降维,同时也是一种特征融合算法. 对于正交属性空间(对2维空间即为直角坐标系)中的样本点 ...

  10. Bootstrap日期插件中文实现

    Bootstrap的相关JS和CSS直接跳过. <script type="text/javascript" src="static/js/jquery-1.9.1 ...