Check for existance of a file in Sharepoint

I was recently writing some simple code to extract a file from a document library. The code was simple enough, but it was the return value that got me into trouble.

The code is as follows:

try
{
  using (SPSite site = new SPSite(http://blah.com))
  {
    using (SPWeb web = site.RootWeb())
    {
        SPFile file = web.GetFile("my file url");
    }
  }
}

All good so far. However, using my .Net brain is did a 'if (file != null)' .... oops.
The file object is never null.

The correct way to check the result if (if (file.Exists) .....).

Simple.

Comments

Popular posts from this blog

SharePoint 2013: Error updating managed account credentials

CISM Exam Prep - Recommended Learning