美文网首页
.net 6 C#中System.IO.Path类的用法

.net 6 C#中System.IO.Path类的用法

作者: fanyu__ | 来源:发表于2024-04-17 11:29 被阅读0次

1. 说明

/*
Performs operations on System.String instances that contain file 
or directory path information. 
These operations are performed in a cross-platform manner.

对系统执行操作。包含文件或目录的字符串实例路径信息。
这些操作是以跨平台的方式执行的。
*/

2. 属性

2.1 AltDirectorySeparatorChar

/*
Provides a platform-specific alternate character 
used to separate directory levels
in a path string that reflects a hierarchical file system organization.

提供用于分隔目录级别的特定于平台的备用字符
在反映分层文件系统组织的路径字符串中。
*/
Console.WriteLine(Path.AltDirectorySeparatorChar); 
// 输出结果:/

2.2 DirectorySeparatorChar

/*
Provides a platform-specific character 
used to separate directory levels in a
path string that reflects a hierarchical file system organization.

提供一个特定于平台的字符,
用于在中分隔目录级别反映分层文件系统组织的路径字符串。
*/
Console.WriteLine(Path.DirectorySeparatorChar); 
// 输出结果:\

2.3 VolumeSeparatorChar

/*
Provides a platform-specific volume separator character.

提供特定于平台的卷分隔符字符。
*/
Console.WriteLine(Path.VolumeSeparatorChar); 
// 输出结果::

3. 方法

3.1 GetFileName

var file= "D:\\data\\files\\测试文件.pdf";
 Console.WriteLine(Path.GetFileName(file));
// 输出结果:测试文件.pdf

3.2 GetFileNameWithoutExtension

var file= "D:\\data\\files\\测试文件.pdf";
 Console.WriteLine(Path.GetFileNameWithoutExtension(file));
// 输出结果:测试文件

3.3 GetDirectoryName

var file= "D:\\data\\files\\测试文件.pdf";
 Console.WriteLine(Path.GetDirectoryName(file));
// 输出结果:D:\\data\\files

3.4 GetExtension

var file= "D:\\data\\files\\测试文件.pdf";
 Console.WriteLine(Path.GetExtension(file));
// 输出结果:.pdf

3.5 GetFullPath

var file= "D:\\data\\files\\测试文件.pdf";
 Console.WriteLine(Path.GetFullPath(file));
// 输出结果:D:\\data\\files\\测试文件.pdf

3.6 GetRelativePath

 var basePath = @"C:\Users\Example\Documents";
 var targetPath = @"C:\Users\Example\Documents\Projects\ExampleProject";

 Console.WriteLine(Path.GetRelativePath(basePath, targetPath));

// 输出结果:Projects\ExampleProject

3.7 GetTempFileName


// 在磁盘上创建一个唯一命名的零字节大小的临时文件,
并返回完整的该文件的路径。

Console.WriteLine(Path.GetTempFileName());

// 输出结果:C:\Users\xxx\AppData\Local\Temp\tmpD10E.tmp

3.8 GetTempPath


// 返回当前用户的临时文件夹的路径。

Console.WriteLine(Path.GetTempPath());

// 输出结果:C:\Users\xxx\AppData\Local\Temp\

3.9 GetPathRoot


// 从指定字符串中包含的路径获取根目录信息。

var file= "D:\\data\\files\\测试文件.pdf";
Console.WriteLine(Path.GetPathRoot());

// 输出结果:D:\

3.10 GetRandomFileName


// 返回随机的文件夹名或文件名。。

Console.WriteLine(Path.GetRandomFileName());

// 输出结果:a54n1pir.yw3

3.11 ChangeExtension


// 修改扩展名。
var file= "D:\\data\\files\\测试文件.pdf";

Console.WriteLine(Path.ChangeExtension(file, ".docx"));

// 输出结果:D:\data\files\测试文件.docx

相关文章

  • ASP.NET C#中Application的用法教程

    这篇文章主要给大家介绍了关于ASP.NET C#中Application的用法,在介绍Application的用法...

  • ASP.NET C#中Application的用法教程

    这篇文章主要给大家介绍了关于ASP.NET C#中Application的用法,在介绍Application的用法...

  • C#中as的用法

    c# 中 as 用法 : 这句代码等价于: 意思是判断实例对象是否属于某个类 完整代码:

  • C#类的访问修饰符用法分析

    这篇文章主要介绍了C#类的访问修饰符用法,较为详细的分析了C#类的访问修饰符概念与用法,具有一定的参考借鉴价值,需...

  • C# abstract virtual sealed

    参考简单易懂的解释c#的abstract和virtual的用法和区别[https://blog.csdn.net/...

  • C#中abstract的用法详解

    C#中abstract的用法详解 abstract可以用来修饰类,方法,属性,索引器和时间,这里不包括字段. 使用...

  • C# 入门

    本文分三个部分.NET 体系结构根据实际项目理解.NET的分层架构C#的一些用法 一、.NET 体系结构 1、基本...

  • c#与objective-c

    C#是微软推出的一种基于.NET框架的、面向对象的高级编程语言。C#以.NET框架类库作为基础,拥有类似Visua...

  • C#入门数据库增删改查

    在C#中ADO.NET技术提供了对数据源的访问,ADO.NET是一个类库,包含了Conection 对象、Coma...

  • 使用GRPC

    C# .NET Framework 对于C# .NET Framework平台,使用GRPC for C#,GRP...

网友评论

      本文标题:.net 6 C#中System.IO.Path类的用法

      本文链接:https://www.haomeiwen.com/subject/gjbsxjtx.html