For some obscure reason, Windows Sharepoint Services allows me to fetch a non-existent file. Consider this code:
SPFile file = site.RootWeb.GetFile(file_url);
For some obscure reason, Windows Sharepoint Services allows me to fetch a non-existent file.
Consider this code:
This code will return an SPFile object regardless of whether file_url points to an existing file or not.
To find out if the file exists, you need to call the file.Exists() method.
Even if we ignore the fact that it’s against the common practice of throwing exceptions for these situations, it still behaves oddly.
You would expect that the properties for a non-existent file would be null, or at least throw exceptions – and indeed, most of them do throw (more on that later), but some of them DON’T – properties like the Name and URL of the file return values that are derived from the original URL I passed it, even though they do not, in fact, point to an existing file.
And the other properties DO throw, but they either throw a System.ArgumentOutOfRangeException, which makes absolutely no sense, or else a SPException specifying that the document library doesn’t exist. The same property can throw a different one of the above exceptions, depending on whether the URL we gave it points to a non-existing file in an existing doc library, or to a doc library that doesn’t exist at all.
It’s a mess, I tell you – a mess.