开发者

Is there a class for holding a file system path?

开发者 https://www.devze.com 2022-12-15 23:42 出处:网络
In the .NET base class library there is a class System.IO.Path for doing common operations on strings representing a file system path. However, what I need is a class that encapsulates a path instead,

In the .NET base class library there is a class System.IO.Path for doing common operations on strings representing a file system path. However, what I need is a class that encapsulates a path instead, so I get type-safety and a possibly shorter notation of path operations. I'm thinking of a .NET equivalent of C++ Boost's path class. Does such a class exist?

Update: I'm not necessarily looking for a class that can hold both file and directory paths. However, as a file system path can be used for pointing to both, I find it obvious that same class can be used.

Conclusion: DirectoryInfo and FileInfo come close to wha开发者_JS百科t I'm looking for. However, they seem to be intended as a representation of a file or directory, rather than a file or directory path. This makes it difficult to do path operations, such as combining a directory path and a relative file path, so I think I'll write a class that encapsulates a path.


You are looking for the System.IO.DirectoryInfo class.

The DirectoryInfo class derives from the abstract [FileSystemInfo] class and you also have the FileInfo class that describes files.


Depending on your specific problem, you could to use Uri class as it can also to represent local addresses.

But, as Rune said (+1) you probably should go with DirectoryInfo


There's no such class as part of the .NET Framework. I suggest you write your own, perhaps based on StringBuilder and the Path class.


I think the mindset in .NET is not to bind a file path to a protocol.

For example, File class is used statically. The identity of a file path is passed around as a string, and then when you need to find out, you do the static method File.exists(pathstring).

I prefer the mindset in .NET over Java's having an instantiable File class. Because once you have an instantiable File class, everybody expects a File object to be passed around.

Normally, a file identity is best passed around as a string because it goes through multiple string mutations and checking and comparison, where most comparisons/mutation is best and efficiently done thro regex. Having a file identity passed around as a File object sucks up cpu because you have to extract the string and if modified you have to reinstantiate a new File object.

Frequently, in Java, I find that I have to convert a File object to string back and forth and back and forth incessantly, because other programmers or library providers have an obsessive/compulsive love affair with the File class.

0

精彩评论

暂无评论...
验证码 换一张
取 消