When is it okay to check if a file exists?

前端 未结 18 2195
再見小時候
再見小時候 2020-11-28 06:52

File systems are volatile. This means that you can\'t trust the result of one operation to still be valid for the next one, even if it\'s the next line of code. You can\'t

相关标签:
18条回答
  • 2020-11-28 07:17

    Your problem could easily be solved with basic computer science... read up on Semaphores.

    (I did not mean to sound like a jerk, I was just pointing you to a simple answer for a common problem).

    0 讨论(0)
  • 2020-11-28 07:18

    The File.Exists method exists primarily for testing for the existence of a file when you do not intend to open the file. For example testing for the existence of a locking file whose very existence tells you something but whose contents are immaterial.

    If you are going to open the file then you will need to handle any exception regardless of the results of any prior calls to File.Exists. So, in general, there is no real value in calling it in these circumstances. Just use the appropriate FileMode enumeration value in your open method and handle any exceptions, as simple as that.

    EDIT: Even though this is couched in terms of the .Net API, it is based on the underlying system API. Both Windows and Unix have system calls (i.e. CreateFile) that use the equivalent of the FileMode enumeration. In fact in .Net (or Mono) the FileMode value is just passed through to the underlying system call.

    0 讨论(0)
  • 2020-11-28 07:18

    In *nix environment a well established method for checking if another copy of the program is already running is to create a lock file. So the check for file existence is used to verify this.

    0 讨论(0)
  • 2020-11-28 07:19

    There are a numbers of possible applications you may well be writing that a simple File.Exists is more than adequate for the job. If it's a config file that only your application will use then you do not need to go so overkill in your exception handling.

    Whilst the "flaws" you have pointed out in using this method are all valid, it doesn't mean they are not acceptable flaws for some situations.

    0 讨论(0)
  • 2020-11-28 07:19

    To answer my own question (in part), I want to expand on the example I used: a default config file.

    Rather than check if it exists at app startup and try to copy the file if the check fails, the thing to do is always try to copy the file. You just do it in such a way that the copy will fail if the file exists rather than replace an existing file. This way all you need to do is catch and ignore any exception thrown if the copy fails because of an existing file.

    0 讨论(0)
  • 2020-11-28 07:21

    One example: You may be able to check for existence of files which you are unable to open (due to, for example, permissions).

    Another, possibly better example: You want to check for the existence of a Unix device file. But definitely do not open it; opening it has side effects (e.g., open/close /dev/st0 will rewind the tape)

    0 讨论(0)
提交回复
热议问题