Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>See this question:<br> <a href="https://stackoverflow.com/questions/265953/c-how-can-you-easily-check-if-access-is-denied-for-a-file">how can you easily check if access is denied for a file in .NET?</a></p> <p>The short version of that question is that you don't, because the file system is volatile. Just try to open the file and catch the exception if it fails.</p> <p>The reason your <code>isFileFound</code> method doesn't work is because the <code>FileInfo</code> structure you are using can also be used to create files. You can create a FileInfo object with the desired info for a non-existing file, call it's <code>.Create()</code> method, and you've set your desired properties all at once.</p> <p>I suspect the reason the UNC path fails is either 1) a permissions issue accessing the admin share from the user running your app, <em>or</em> 2) The <code>$</code> symbol is throwing the method off, either because it's not being input correctly or because of a bug in the underlying .Exists() implementation.</p> <p><strong>Update:</strong> </p> <p>When I post this suggestion, I nearly always get a complaint about exception performance. Let's talk about that. Yes, handling exceptions is expensive: <em>very</em> expensive. There are few things you can do in programming that are slower. But you know what one those few things is? Disk and network I/O. Here's a link that demonstrates just how much disk I/O and network I/O cost: </p> <blockquote> <p><a href="https://gist.github.com/jboner/2841832" rel="nofollow noreferrer">https://gist.github.com/jboner/2841832</a></p> </blockquote> <pre>Latency Comparison Numbers -------------------------- L1 cache reference 0.5 ns Branch mispredict 5 ns L2 cache reference 7 ns 14x L1 cache Mutex lock/unlock 25 ns Main memory reference 100 ns 20x L2 cache, 200x L1 cache Compress 1K bytes with Zippy 3,000 ns Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms Read 4K randomly from SSD* 150,000 ns 0.15 ms Read 1 MB sequentially from memory 250,000 ns 0.25 ms Round trip within same datacenter 500,000 ns 0.5 ms Read 1 MB sequentially from SSD* 1,000,000 ns 1 ms 4X memory Disk seek 10,000,000 ns 10 ms 20x datacenter roundtrip Read 1 MB sequentially from disk 20,000,000 ns 20 ms 80x memory, 20X SSD Send packet CA->Netherlands->CA 150,000,000 ns 150 ms</pre> <p>If thinking in nanoseconds isn't your thing, here's another link that normalizes one CPU cycle as 1 second and scales from there:</p> <blockquote> <p><a href="http://blog.codinghorror.com/the-infinite-space-between-words/" rel="nofollow noreferrer">http://blog.codinghorror.com/the-infinite-space-between-words/</a></p> </blockquote> <pre> 1 CPU cycle 0.3 ns 1 s Level 1 cache access 0.9 ns 3 s Level 2 cache access 2.8 ns 9 s Level 3 cache access 12.9 ns 43 s Main memory access 120 ns 6 min Solid-state disk I/O 50-150 μs 2-6 days Rotational disk I/O 1-10 ms 1-12 months Internet: SF to NYC 40 ms 4 years Internet: SF to UK 81 ms 8 years Internet: SF to AUS 183 ms 19 years OS virt. reboot 4 s 423 years SCSI command time-out 30 s 3000 years Hardware virt. reboot 40 s 4000 years Physical system reboot 5 m 32 millenia</pre> <p>Taking even the best-case scenario for exceptions, you can access memory at least 480 times while waiting on the first response from a disk, and that's assuming a very fast SSD. Many of us still need spinning hard-drives, where things get much, much worse.</p> <p>And that's only the beginning of the story. When you use <code>.Exists()</code>, you incur this <em>additional</em> cost (and it is an addition: you have to do the same work again when you go to open the file) on <em>every</em> attempt. You pay this costs whether the file exists or not, because the disk still has to go look for it in it's file tables. With the exception method, you only pay the extra cost of unwinding the call stack in the case of failure.</p> <p>In other words, yes: exceptions are horribly costly. But compared to the disk check, it's still faster: and not by just a small margin. Thankfully, this is unlikely to drive your app's general performance... but I still want to put to bed the "exceptions are slow" argument for this specific task.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload