SharePoint: urlOfFile Parameter name: Specified value is not supported for the urlOfFile parameter.


I needed to check whether a file exists inside a folder in document library, so I wrote the following code:

SPList list = web.Lists["DOC1"];
string fileName = "Sample.docx";
if (!list.RootFolder.Files[string.Format("{0}/{1}", list.RootFolder.Url, fileName)].Exists)
{
  // Code Implementation
}
After running this code, I got the following error, 
urlOfFile Parameter name: Specified value is not supported for the urlOfFile parameter.
It turned out that the proper way of doing this is to use the GetFile method of SPWeb object like this:
SPFile file = web.GetFile(string.Format("{0}/{1}", list.RootFolder.Url, fileName));
if (file.Exists)
{
 // Code Implementation
}

profile for Nadeem Yousuf at SharePoint Stack Exchange, Q&A for SharePoint enthusiasts

+