C# Path类常用方法
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:^*&)(_=@#'\\^.*(.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:^*&)(_=@#'\^.*(.txt' and 'subdir\file.txt', the result is:
// 'c:^*&)(_=@#'\^.*(.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类常用方法的更多相关文章
- C# IO操作(一)Path类的常用方法
1.Path类,查看编译器可知,这个类是个静态的工具类,需要注意的是,这个类是对字符串的操作,与文件无关. 1)ChangeExtension()方法,修改文件的后缀(调用这个方法,如果给第二个参数制 ...
- java基础-System类常用方法介绍
java基础-System类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.System类概念 在API中system类介绍的比较简单,我们给出定义,system中 ...
- 04.Path类的学习
path 是路径的意思. path类是一个静态类,所以path是一个工具类. Path类是专门用来操作路径的. Path的常用方法: namespace _15.Path类的学习 { class Pr ...
- 使用File类、StreamRead和StreamWrite读写数据、以及Path类操作文件路径和Directory
1.File类的概念: File类,是一个静态类,主要是来提供一些函数库用的.静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和 打开一个文件. File类方法的参量 ...
- Android中Path类的lineTo方法和quadTo方法画线的区别
转载:http://blog.csdn.net/stevenhu_223/article/details/9229337 当我们需要在屏幕上形成画线时,Path类的应用是必不可少的,而Path类的li ...
- .net学习之集合、foreach原理、Hashtable、Path类、File类、Directory类、文件流FileStream类、压缩流GZipStream、拷贝大文件、序列化和反序列化
1.集合(1)ArrayList内部存储数据的是一个object数组,创建这个类的对象的时候,这个对象里的数组的长度为0(2)调用Add方法加元素的时候,如果第一次增加元神,就会将数组的长度变为4往里 ...
- Android -- 自定义View小Demo,关于Path类的使用(一)
1,在我们知道自定义view中onDraw()方法是用于绘制图形的,而Path类则是其中的一个重要的类,如下图效果: 代码也没有什么难度,直接贴出来吧 @Override protected void ...
- Java 7 中 NIO.2 的使用——第一节 Path 类的使用
路径隶属于文件系统,实际上它是存储和组织媒体文件的格式,通常在一块或多块硬盘设备上,以便于非常容易地检索.文件系统可以通过 java.nio.file.FileSystems 这个final 类来访 ...
- 文件夹和文件、Path类、流、序列化
循环访问目录树 参考: http://msdn.microsoft.com/zh-cn/library/bb513869.aspx 循环访问目录树”的意思是在指定的根文件夹下,访问每个嵌套子目录中任意 ...
随机推荐
- keytool生成JKS证书的详细步骤及截图
注:防止有不必要的空格,尽量不要复制粘贴 1. 依据CFCA所提供的CN生成密钥存储文件和密钥对(创建JKS证书库) keytool -genkey -v -alias slserver -keyal ...
- 各种Java项目环境搭建-文档引用汇总记录
springmvc环境搭建 1.如何用Maven创建web项目(具体步骤) 2.springmvc环境搭建,一步一步超简单
- DevOps介绍
DevOps 也同样要通过技术工具链完成持续集成.持续交付.用户反馈和系统优化的整合.Elasticbox 整理了 60+ 开源工具与分类,其中包括版本控制&协作开发工具.自动化构建和测试工具 ...
- python's twenty-fifth day for me 模块
模块: py文件就是模块. python之所以好用,因为模块多. 内置模块:python安装的时候自带的. 扩展模块:别人写好的,需要安装后可直接使用. 自定义模块:自己写的模块. 序列化模块: 能存 ...
- leetcode867
vector<vector<int>> transpose(vector<vector<int>>& A) { vector<vector ...
- InputStreamReader和BufferedReader的区别
.InputStream.OutputStream 处理字节流的抽象类 InputStream 是字节输入流的所有类的超类,一般我们使用它的子类,如FileInputStream等. OutputSt ...
- Java中包、类、方法、属性、常量的命名规则
1:包(package):用于将完成不同功能的类分门别类,放在不同的目录(包)下,包的命名规则:将公司域名反转作为包名.比如www.baidu.com 对于包名:每个字母都需要小写.比如:com.ba ...
- 如何解决quartz在集群下出现的资源抢夺现象
Quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度,简单的说就是可以 ...
- 23-从零玩转JavaWeb-单例设计模式
一.什么是设计模式 二.什么是单例设计模式 三.单例设计模式特点 四.单例设计模式优点 五.单例设计模式实现步骤 六.什么是工具类
- Pacemaker实现双机热备
在互联网高速发展的今天,尤其在电子商务的发展,要求服务器能够提供不间断服务.在电子商务中,如果服务器宕机,造成的损失是不可估量的.要保证服务器不间断服务,就需要对服务器实现冗余.在众多的实现服务器冗余 ...