开发者

A question about checking for the existence of a file versus the directory being empty and reliability

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


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜