tags:

views:

80

answers:

4

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 what 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.

+6  A: 

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.

Rune Grimstad
Indeed, this seems to be appropriate for directory paths. Is there also a class that can hold a path to a file?
Dimitri C.
I updated my answer. Check the FileInfo class.
Rune Grimstad
+1  A: 

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.

John Saunders
Doesn't the DirectoryInfo do this? :-)
Rune Grimstad
Does it work for paths that don't exist? If he meant to limit himself to actual directories, then I wish he'd update the question to say so.
John Saunders
It has an Exists property and a Create method, so yes it works on directories that doesn't exist.
Rune Grimstad
+1  A: 

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

Rubens Farias
A: 

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.

Blessed Geek