简体   繁体   English

C#(非静态)类来表示路径

[英]C# (non-static) class to represent paths

I'm looking for a C# class that represents a file system path . 我正在寻找一个代表文件系统路径的C#类。 I would like to use it (instead of strings) as the data type of variables and method arguments (top reasons: type safety, concat-proof, logical comparisons). 我想使用它(而不是字符串)作为变量和方法参数的数据类型(最主要的原因:类型安全,防连接,逻辑比较)。

  • System.IO.Path provides most of the functionality I want, but it is static. System.IO.Path提供了我想要的大部分功能,但它是静态的。
  • System.IO.FileInfo, as I understand, performs IO operations to do its job. 据我所知,System.IO.FileInfo执行IO操作来完成它的工作。 I only want a wrapper for the path string. 我只想要路径字符串的包装器。

Thanks! 谢谢!

May be the library NDepend.Helpers.FileDirectoryPath is what you are looking for. 可能是库NDepend.Helpers.FileDirectoryPath就是你要找的。

It provides: 它提供:

  • Strongly typed File/Directory path. 强类型文件/目录路径。
  • Relative / absolute path conversion. 相对/绝对路径转换。
  • Path normalization API 路径规范化API
  • Path validity check API 路径有效性检查API
  • Path comparison API 路径比较API
  • Path browsing API. 路径浏览API。
  • Path rebasing API 路径重定向API
  • List of path operations (TryGetCommonRootDirectory, GetListOfUniqueDirsAndUniqueFileNames, list equality…) 路径操作列表(TryGetCommonRootDirectory,GetListOfUniqueDirsAndUniqueFileNames,列表相等...)

Well, what you're asking for is System.Uri . 那么,你要求的System.Uri But what you want to do (or more accurately, what you should do) is use System.Path and friends. 但是你想要做的事情(或者更确切地说,你应该做什么)是使用System.Path和朋友。 It's what everyone else does, and it's what is correct and appropriate. 这是其他人所做的,这是正确和恰当的。

First of all, System.IO.Path is static , not abstract . 首先, System.IO.Pathstatic ,而不是abstract

Depending on what exactly you want to do, you could either use System.IO.DirectoryInfo , System.Uri , or create a custom class that uses System.IO.Path internally. 根据您要执行的操作,您可以使用System.IO.DirectoryInfoSystem.Uri ,也可以创建在内部使用System.IO.Path的自定义类。

为System.IO.Path构建一个包装器,并将缺少的功能添加到您的包装器中。

System.IO.Path is not an abstract class, it's a static class; System.IO.Path不是一个抽象类,它是一个静态类; the difference being that you can use the static Path methods to manipulate strings that represent file paths. 不同之处在于您可以使用静态Path方法来处理表示文件路径的字符串。

You can also use the System.Uri class for some of the file path operations, but that class really targets URLs, not files. 您还可以将System.Uri类用于某些文件路径操作,但该类实际上是针对URL而不是文件。

If you want to be fancy, or you need your file path manipulation methods to be attached to a specific instance, you can write a set of extension methods to the String class that perform file path operations using the Path methods internally. 如果您想要花哨,或者需要将文件路径操作方法附加到特定实例,则可以向String类编写一组扩展方法,以在内部使用Path方法执行文件路径操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM