关于检查文件是否存在与目录是否为空以及可靠性的问题

发布于 2024-09-28 09:50:25 字数 414 浏览 7 评论 0原文

我知道几乎每种编程语言都有一种方法来检查文件或目录是否存在。

然而,就我而言,创建了一个存储程序设置的文件。如果不存在(即 !File.Exists 或 Directory.Count == 0,其中 Directory 是文件的包含目录),则提示填写一些设置。

但是,这是某种代码可靠吗?例如,在请求详细信息的情况下,目录中可能没有文件,否则可能存在不相关的其他类型的文件或正确格式的被篡改的文件。检查特定文件本身会更好吗?检查构成文件的变量是否会更好,因为这样更快?

这样做的最佳一般方法是什么?检查文件,该文件夹是否存在?如果该文件夹存在并且是空的?检查写入文件的字符串?

编辑:一位同事的一个思想流派是,我应该在变量级别进行检查,因为它更接近问题(识别诸如不正确的加密、损坏、区域设置等问题)。

谢谢

I know that pretty much every programming language has a method to check the existence of a file or directory.

However, in my case, a file is made which stores program settings. If it does not exist (ie !File.Exists or Directory.Count == 0 where Directory is the containing directory of the file), then prompt for some settings to be filled in.

However, is this sort of code reliable? For example, there may be no files in the directory in which case the details are requested, otherwise there may be files of other types which are irrelevant or a tampered file of the correct format. Would it be better to check for the specific file itself? Would it also be better to check for variables which make up the file as this is faster?

What is the best general way of doing this? Check a file, if the folder is there? If the folder is there and empty? Check the strings which are written to the file?

EDIT: A school of thought from a colleague was that I should check at the variable level because it would be closer to the problem (identify issues like incorrect encryption, corruption, locales, etc).

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Hello爱情风 2024-10-05 09:50:25

我只是检查特定文件的存在性和有效性。如果您遇到损坏的文件,请将其删除并创建一个新文件。

I'd just check for the existence and validity of the specific file. If you encounter a corrupted file, get rid of it and make a new one.

深海里的那抹蓝 2024-10-05 09:50:25

对于基础开发来说,纯粹是选择。在文件存在对于应用程序的稳定性至关重要的情况下,直接检查文件是最安全的途径。

For basic development, it is purely choice. In the case where the file existing is crucial to the stability of the application, checking the file directly is the safest route.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文