[英]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).
我想使用它(而不是字符串)作为变量和方法参数的数据类型(最主要的原因:类型安全,防连接,逻辑比较)。
Thanks! 谢谢!
May be the library NDepend.Helpers.FileDirectoryPath is what you are looking for. 可能是库NDepend.Helpers.FileDirectoryPath就是你要找的。
It provides: 它提供:
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.Path
是static
,而不是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.DirectoryInfo
, System.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.