6

I am making a command line utility. I need to pass around a few file paths in the program.

Right now I am just using string. But I was wondering if the .net library had a cool class for passing around file paths.

5 Answers 5

8

Well, there's FileInfo - that's about as close as you get.

Sign up to request clarification or add additional context in comments.

1 Comment

I used FileInfo in some previous projects, it's really nice, if you just want to read a file, use a string, If you want to manage files, folders and do some actions with them, use FileInfo
7

Strings are the best way to store paths.

If you need to modify or work with the path, though, you should use System.IO.Path.

If you want something that is a little more robust and can actually interact with files/directories you could also check out FileInfo and DirectoryInfo. Keep in mind, though, that both of these are awfully heavy if you just need to store the path.

2 Comments

Are they heavy? What's the overhead?
The overhead is that instead of a simple string, you have a big ol' object with a number of member fields that take up memory. There is also the cost of object creation. The constructor, especially, includes a number of expensive calls, some of which call outside of the .NET framework and might even result in disk I/O.
3

The Base Class Library represents paths as strings but there are open source alternatives. NDepend.Helpers.FileDirectoryPath is a open source library for manipulating paths in a strongly typed way.

Comments

0

DirectoryInfo objects can hold filepaths and any relevant information about the directory, FileInfo can hold information and path of individual files as well.

Comments

0

Justin Niessner answer is very good... but just to add on that: System.Io.Path also has some static methods that can help, so we don't have to worry about extra "slashes" and cross-platform differences (back/forward slash for example).

Ex.: you can use .Join (or .Combine):

var readFile = new StreamReader(Path.Join(filePath, inputFileName));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.