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. Jenkins的用户管理

    用户管理入口 Jenkins首页有一个用户,但是只能从那查看用户列表和信息,管理用户的入口在Jenkins->系统管理->管理用户 新建用户 在管理用户左侧有一个新增用户,点击后按照表单填 ...

  2. redis的安装和类型及基本命令

    一.memcached和redis区别 1. redis 可以存储,memcached用来缓存, 2. 数据类型,memcached只有string:redis有string,链表,哈希结构,集合,有 ...

  3. HTTP-头域

    头域 每个头域由一个域名,冒号(:)和域值三部分组成.域名是大小写无关的,域值前可以添加任何数量的空格符,头域可以被扩展为多行,在每行开始处,使用至少一个空格或制表符. HTTP请求消息 HTTP状态 ...

  4. awk常见基本使用

    -F 指定分割符号 print 外层的引号必须是单引号 $n不能被解析 [root@bogon ~]# .txt a:b:c:d a1:b1:c1:d1 a2:b2:c2:d2 a_: :c:dddd ...

  5. PS相关技术

    PS相关长时间不用就忘记了,做个笔记,记录下来 (1)复制图层,可以将图层复制到另外的图层里去,这样,多个图层就可以编辑了 (2)通过建立选区,可以选择右键,通过剪切的图层,通过复制的图层将图片抠出来 ...

  6. Oracle 利用执行计划来避免排序操作

    在oracle中,利用index来避免排序 SQL) NOT NULL); SQL> CREATE INDEX IND_T_NOSORT_NAME ON T_NOSORT(NAME); SQL& ...

  7. Docker - 避免启动container后运行shell脚本执行完成后docker退出container

    问题 最近在使用 Dockerfile 启动容器,发现使用Dockerfile调用容器里面的shell,当shell执行完成以后,docker会退出容器. 分析 Docker 在执行shell的时候, ...

  8. krpano之鼠标样式修改

    引入cursors.js. cursors.js代码: <krpano > <events onxmlcomplete="action(qtvrcursor);" ...

  9. Catch That Cow(bfs)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  10. cxf和axis2使用有感

    CXF框架 个人不喜欢使用wsimport工具: 1.考虑到远端的服务接口发生变化,本地的接口还需要重新同步下 2.项目中无端多了些冗余的代码 这样我们选择cxf的动态调用接口吧,使用DynamicC ...