views:

36

answers:

2

Hi,

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

+2  A: 

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.

Sam Dufel
A: 

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.

Evan Mulawski