开发者

Is there a better way to hold a file path than in a string?

开发者 https://www.devze.com 2023-01-09 10:34 出处:网络
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 fo

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.


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


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.


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.


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


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));
0

精彩评论

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