I'm being completely confused here folks,
My code throws an exception because File.Exists() returns false
public override sealed TCargo ReadFile(string fileName)
{
if (!File.Exists(fileName))
{
throw new ArgumentException("Provided file name does not exist", "fileName");
}
Visual studio breaks at the throw statement, and I immediately check the value of File.Exists(fileName)
in the immediate window. It returns true
. When I drag the breakpoint back up to the if statement and execute it again, it throws again.
fileName is an absolute path to a file. I'm not creating the file, nor writing to it (it's there all along). If I paste the path into the open dialog in Notepad, it reads the file without problems.
The code is executing in a background worker. It's the only complicating factor I can think of. I am positive the file has not been opened already, either in the worker thread or elsewhere.
What's going on here?
I don't know what's going on, but why do you need the File.Exists test at all? What you're really interested in is, "Can I read this file?" Plenty of other things other than File Not Found can go wrong.
Not to mention, doing a File.Exists test is a race condition because the file could go away after you've done the test, but before you open the file. Just open the file, that's the best test you can do to determine whether you can read the file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With