tags:

views:

101

answers:

5

I'm looking for a C# class that represents a file system path. 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.FileInfo, as I understand, performs IO operations to do its job. I only want a wrapper for the path string.

Thanks!

+2  A: 

Well, what you're asking for is System.Uri. But what you want to do (or more accurately, what you should do) is use System.Path and friends. It's what everyone else does, and it's what is correct and appropriate.

Noon Silk
+1  A: 

First of all, System.IO.Path is static, not 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.

Willem van Rumpt
A: 

Build a wrapper for System.IO.Path and add the missing functionality to your wrapper.

Egeria
Naturally I could do that, but I would like to avoid writing NIH code. It's a solved problem.
A: 

System.IO.Path is not an abstract class, it's a static class; the difference being that you can use the static Path methods to manipulate strings that represent file paths.

You can also use the System.Uri class for some of the file path operations, but that class really targets URLs, not files.

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.

Franci Penov
+1  A: 

May be the library NDepend.Helpers.FileDirectoryPath is what you are looking for.

It provides:

  • Strongly typed File/Directory path.
  • Relative / absolute path conversion.
  • Path normalization API
  • Path validity check API
  • Path comparison API
  • Path browsing API.
  • Path rebasing API
  • List of path operations (TryGetCommonRootDirectory, GetListOfUniqueDirsAndUniqueFileNames, list equality…)
Jehof
I'll look into this one, it looks promising. Thanks!
Didn't end up using it because of time constraints (couldn't afford necessary refactoring). This library was, however, exactly what I was looking for.