SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to save file from network location to SharePoint document library.

This is code of my method:

//parameters: string documentLibraryUrl, string filePath

FileInfo fi = new FileInfo(filePath);
string fileName = fi.Name.Trim();       
using (SPSite oSite = new SPSite(documentLibraryUrl))
    using (SPWeb oWeb = oSite.OpenWeb())
        SPFolder myLibrary = oWeb.GetFolder(documentLibraryUrl);

        // Prepare to upload
        using (FileStream fileStream = File.OpenRead(filePath))
            // Upload document

            SPFile spFile = oWeb.GetFile(myLibrary.Url + "/" + fileName);
            filePathInLibrary = spFile.ParentFolder.ServerRelativeUrl + "/" + fileName;
            spFile.SaveBinary(fileStream);      //this line throws exception

            // Commit 


When I call SPFile.SaveBinary, I get following DirectoryNotFoundException:

<nativehr>0x80070003</nativehr><nativestack></nativestack>There is no file with URL "http://localhost:9999/Lists/Documents/testfile.pdf" in this Website.

The fileStream variable is not null and has Length > 0. The list exists.

What could be wrong?

So after Amit's reply, I changed the code from line:


to this block:

if (spFile.Exists)
    //do some logging here
    spFile = myLibrary.Files.Add(myLibrary.Url + "/" + fileName, fileStream);

In fact I get exactly the same error message on myLibrary.Files.Add.

What else could be the reason of this error? The code is probably correct, because it worked on different server. I'm thinking about permissions - can it be, that I need extra permissions on SharePoint or on SQL server?
I'm able to add file to the list in UI (in browser) without problems. I'm using Claims Based Authentication. My user (with Windows identity) has Full Control permissions on Web Application.

share|improve this question
For posterity, the line if (spFile.Exists) should not be used / depended upon for an existence check if you use SPWeb.GetFile() as the results of this property are inconsistent at best: – RJ Cuthbertson Dec 26 '12 at 18:52
I'm getting this error using SPWeb.GetFile and the file definitely exists. Any luck on resolving? – ExceptionLimeCat Jul 22 '15 at 17:32

In above code SPWeb.GetFile don't give you a file as it does not exist (because you are copying a new file)

SPFile.SaveBinary is normally used to update exisitng file. To add a new file you should use SPFolder.Files.Add

share|improve this answer
I changed my code as you suggested, but unfortunately, I get the same error. See my update. – Tschareck Apr 16 '12 at 11:34
up vote 1 down vote accepted

Thank you all for your answers. You wouldn't believe how silly and hard to find this error was.

The myLibrary's Url was:


while the siteUrl, that was passed to my method was


This resulted in double slash between server name and list relative path.

share|improve this answer
You could use url combining to avoid these type of bugs: – Janis Veinbergs May 14 '12 at 8:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.