翼度科技»论坛 编程开发 .net 查看内容

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

3

主题

3

帖子

9

积分

新手上路

Rank: 1

积分
9
1. 说明
  1. /*
  2. Performs operations on System.String instances that contain file
  3. or directory path information.
  4. These operations are performed in a cross-platform manner.
  5. 对系统执行操作。包含文件或目录的字符串实例路径信息。
  6. 这些操作是以跨平台的方式执行的。
  7. */
复制代码
2. 属性

2.1 AltDirectorySeparatorChar
  1. /*
  2. Provides a platform-specific alternate character
  3. used to separate directory levels
  4. in a path string that reflects a hierarchical file system organization.
  5. 提供用于分隔目录级别的特定于平台的备用字符
  6. 在反映分层文件系统组织的路径字符串中。
  7. */
  8. Console.WriteLine(Path.AltDirectorySeparatorChar);
  9. // 输出结果:/
复制代码
2.2 DirectorySeparatorChar
  1. /*
  2. Provides a platform-specific character
  3. used to separate directory levels in a
  4. path string that reflects a hierarchical file system organization.
  5. 提供一个特定于平台的字符,
  6. 用于在中分隔目录级别反映分层文件系统组织的路径字符串。
  7. */
  8. Console.WriteLine(Path.DirectorySeparatorChar);
  9. // 输出结果:\
复制代码
2.3 VolumeSeparatorChar
  1. /*
  2. Provides a platform-specific volume separator character.
  3. 提供特定于平台的卷分隔符字符。
  4. */
  5. Console.WriteLine(Path.VolumeSeparatorChar);
  6. // 输出结果::
复制代码
3. 方法

3.1 GetFileName
  1. var file= "D:\\data\\files\\测试文件.pdf";
  2. Console.WriteLine(Path.GetFileName(file));
  3. // 输出结果:测试文件.pdf
复制代码
3.2 GetFileNameWithoutExtension
  1. var file= "D:\\data\\files\\测试文件.pdf";
  2. Console.WriteLine(Path.GetFileNameWithoutExtension(file));
  3. // 输出结果:测试文件
复制代码
3.3 GetDirectoryName
  1. var file= "D:\\data\\files\\测试文件.pdf";
  2. Console.WriteLine(Path.GetDirectoryName(file));
  3. // 输出结果:D:\\data\\files
复制代码
3.4 GetExtension
  1. var file= "D:\\data\\files\\测试文件.pdf";
  2. Console.WriteLine(Path.GetExtension(file));
  3. // 输出结果:.pdf
复制代码
3.5 GetFullPath
  1. var file= "D:\\data\\files\\测试文件.pdf";
  2. Console.WriteLine(Path.GetFullPath(file));
  3. // 输出结果:D:\\data\\files\\测试文件.pdf
复制代码
3.6 GetRelativePath
  1. var basePath = @"C:\Users\Example\Documents";
  2. var targetPath = @"C:\Users\Example\Documents\Projects\ExampleProject";
  3. Console.WriteLine(Path.GetRelativePath(basePath, targetPath));
  4. // 输出结果:Projects\ExampleProject
复制代码
3.7 GetTempFileName
  1. // 在磁盘上创建一个唯一命名的零字节大小的临时文件,
  2. 并返回完整的该文件的路径。
  3. Console.WriteLine(Path.GetTempFileName());
  4. // 输出结果:C:\Users\xxx\AppData\Local\Temp\tmpD10E.tmp
复制代码
3.8 GetTempFileName
  1. // 返回当前用户的临时文件夹的路径。
  2. Console.WriteLine(Path.GetTempFileName());
  3. // 输出结果:C:\Users\xxx\AppData\Local\Temp\
复制代码
3.9 GetTempFileName
  1. // 从指定字符串中包含的路径获取根目录信息。
  2. var file= "D:\\data\\files\\测试文件.pdf";
  3. Console.WriteLine(Path.GetTempFileName());
  4. // 输出结果:D:\
复制代码
3.10 GetRandomFileName
  1. // 返回随机的文件夹名或文件名。。
  2. Console.WriteLine(Path.GetRandomFileName());
  3. // 输出结果:a54n1pir.yw3
复制代码
3.11 ChangeExtension
  1. // 修改扩展名。
  2. var file= "D:\\data\\files\\测试文件.pdf";
  3. Console.WriteLine(Path.ChangeExtension(file, ".docx"));
  4. // 输出结果:D:\data\files\测试文件.docx
复制代码
来源:https://www.cnblogs.com/zzuwangzhen/p/18143159
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
来自手机

举报 回复 使用道具