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. NoSuchBeanDefinitionException: No bean named 'shiroFilter' is defined

    以前运行正常的项目,过了一段时间再运行时出问题,打开链接无反应,无法访问Tomcat,空白页面. 经检查发现,在Tomcat log中有报错: NoSuchBeanDefinitionExceptio ...

  2. 6.solr学习速成之multicore查询

    查询关联多个core 再新建一个core 向每个core添加索引,修改 final static String SOLR_URL = "http://localhost:8080/solr/ ...

  3. Apache Shiro 权限框架

    分享一个视屏教程集合 http://www.tudou.com/home/konghao/item 1.Shiro Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码 ...

  4. rtmp发送H264及aac的音视频 (转)

    RTMP推送的音视频流的封装形式和FLV格式相似,由此可知,向FMS推送H264和AAC直播流,需要首先发送"AVC sequence header"和"AAC sequ ...

  5. wireshark怎么抓包、wireshark抓包详细图文教程(转)

    wireshark怎么抓包.wireshark抓包详细图文教程 wireshark是非常流行的网络封包分析软件,功能十分强大.可以截取各种网络封包,显示网络封包的详细信息.使用wireshark的人必 ...

  6. Java对象的强、软、弱和虚引用

    本文介绍Java对象的强.软.弱和虚引用的概念.应用及其在UML中的表示. 1.Java对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象 ...

  7. day58-activiti 13-搭建web项目环境

    Eclipse的项目的build目录不可被删除,删除了也会被自动创建. 到项目的输出路径才看得到编译好的Java类.Eclipse的视图下是看不见的,因为类路径下的这个目录build不想让你操作,它给 ...

  8. Angular问题04 模块导入错误???、BrowserModule模块重复加载???、material模块引入后报错

    1 模块导入错误 1.1 问题描述 项目启动时报错:元数据错误,错误截图如下: 1.2 问题原因 利用VsCode开发angular项目时利用自动导入出现错误 坑01:VsCode 的自动导入功能比较 ...

  9. 189. Rotate Array 从右边开始翻转数组

    [抄题]: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ...

  10. 什么是Kali Linux?

    什么是Kali Linux? Kali Linux是一个基于Debian的Linux发行版,旨在实现高级渗透测试和安全审计.Kali包含数百种工具,适用于各种信息安全任务,如渗透测试,安全研究,计算机 ...