良好的原因传递字符串,而不是使用的DirectoryInfo / FileInfo的路径 [英] Good reasons to pass paths as strings instead of using DirectoryInfo/FileInfo

查看:171
本文介绍了良好的原因传递字符串,而不是使用的DirectoryInfo / FileInfo的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的新code,我的没有的使用字符串传递目录路径和文件名。相反,我现在用的DirectoryInfo和FileInfo的,因为他们似乎封装大量的信息。

In my new code I am not using strings to pass directory paths or file names. Instead I am using DirectoryInfo and FileInfo as they seem to encapsulate a lot of information.

我已经看到了很多的code使用字符串传递的目录信息,那么,他们分裂和中期,并在长期的INCOM prehensible声明指导书,直到他们得到的目录的一部分,他们所期待的。

I have seen a lot of code that uses strings to pass directory information then they "split" and "mid" and "instr" in long incomprehensible statements until they get the part of the directory they are looking for.

有没有传递路径字符串任何正当理由吗?

Is there any good reason to pass paths as strings?

推荐答案

在一般情况下,我想保持信息的FileInfo / DirectoryInfo的比较好。有很多在这些类有用的功能,还有很多安全的参与,它更容易检查存在的一个文件,看到原先指定的文件等。

In general, I think keeping the information in FileInfo/DirectoryInfo is better. There is a lot of useful functionality in these classes, as well as a lot of safety involved in that it's much easier to check for existence of a file, see the originally specified file, etc.

在那里我会(潜在的)传递一个路径作为一个字符串,而不是使用的FileInfo和DirectoryInfo的会,如果路径打算跨应用程序域之间或流程传递,等的地方。

The only place where I would (potentially) pass a path as as a string instead of using FileInfo and DirectoryInfo would if the path was going to be passed across AppDomains or between processes, etc.

的FileInfo和DirectoryInfo的跨AppDomain的界限都工作得不错(因为他们是序列化),但他们在这种情况下,相当数量更多的开销。如果一切进展来回了很多,它可以产生影响。

FileInfo and DirectoryInfo both work fine across AppDomain boundaries (since they're Serializable), but they have a fair amount more overhead in this situation. If things are going back and forth a lot, it could have an impact.

我会用的FileInfo和DirectoryInfo的坚持在这种情况下,虽然,除非我发现有我的分析过程中一个突出的问题,我试图减少序列化的数据量。如果我没有遇到性能问题,我会坚持使用这些类,因为它们提供了大量的安全性和功能性。

I would stick with FileInfo and DirectoryInfo in this case, though, unless I found that there was a noticeable problem during my profiling, and I was trying to reduce the amount of serialized data. If I didn't run into performance issues, I'd stick with using these classes, as they provide a lot of safety and functionality.

这篇关于良好的原因传递字符串,而不是使用的DirectoryInfo / FileInfo的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆