Does the File.Exists method get a lot of resources?

So which is better? To use such a construction:

if (File.Exist(fileName)) { // do something with file... } 

just

 try { // do something with file. } catch(Exception ex) { } 

Is it worth it to use the File.Exist () method a lot?

Thanks!

+4
source share
5 answers

Exceptions should not be used to process your application thread; the idea is to avoid exceptions and not expect them as a normal part of the execution thread.

For 99.999% of applications, if there is a difference in performance, it will not be noticeable. If the file should be there and the search is not an exclusive script, you can use the try catch , otherwise I would say that you should go to the File.Exist approach.

+5
source

The first has a race condition: another process can delete the file after File.Exists returns true, but before opening it. The latter does not. Even if you check ahead of time, you should still catch an exception if you want to ignore unused files.

So it should be either

 if (File.Exists(fileName)) { try { // ... } catch (FileNotFoundException) { } } 

or

 try { // ... } catch (FileNotFoundException) { } 

The former duplicates the check, which can be slow if the file is located in a network resource, the latter throws an exception (which is processed) for a non-exceptional condition, which complicates debugging. Both have their merits. Personally, I generally choose the second, but everything is in order.

+11
source

Depends on the flow of your program and the actions you perform. If you expect the file to exist, you can rely on exception handling, since your program cannot continue if it is not, and the exception will most likely need to be handled higher in the call chain.

Otherwise, you will get True|False|FileNotFound code return madness if the method in question is similar to ReadFile() .

Using File.Exists to "safely" open a file is pretty useless. Consider this:

 public String ReadFile(String filename) { if (!File.Exists(filename)) { // now what? throw new FileNotFoundException()? return null? } // Will throw FileNotFoundException if not exists, can happen (race condition, file gets deleted after the `if` above) using (var reader = new StreamReader(filename)) { return reader.ReadToEnd(); } } 

We can say that you want to check if the file exists if you want to add data to it, but the StreamWriter constructor is overloaded with the append parameter, which will allow the author to create the file if it does not exist and is added to it if it does.

So, perhaps the question might be better: what are the valid use cases for File.Exists ? And, fortunately, this question has already been asked and answered .

+1
source

First I will check if the file exists and throws an exception if it does not exist.

After that, I would use a try-catch block to handle other exceptions that might be thrown (permissions, etc.)

 if (!File.Exist(fileName)) { throw new ArgumentException("filename"); // or throw new FileNotFoundException("filename"); } try { // do something with file. } catch(Exception ex) { } 
0
source

The first case is

 if (File.Exist(fileName)) //A single statement to check and proceed. 

While the later exception handling method includes -

An object of the exception class that must be created and passed to the corresponding catch block if an exception occurs.

I would prefer the first method, since using exception handling to handle the execution thread is not a good idea.

0
source

Source: https://habr.com/ru/post/1447939/


All Articles