I am tired of these exceptions being thrown on production server where I am getting bunch of files from document library and downloading them to a folder directory on server.
Worse thing is that it happens once in 10 maybe or once in 20, it's quiet random there isn't no pattern at all.
I am using this code if I can improve it somehow,
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spSite = new SPSite(siteUrl))
using (SPWeb spWeb = spSite.OpenWeb())
{
SPDocumentLibrary library = spWeb.Lists[listName] as SPDocumentLibrary;
foreach (SPListItem listItem in library.Items)
{
SPFile file = listItem.File;
byte[] document = file.OpenBinary();
System.IO.Directory.CreateDirectory(Path);
System.IO.FileStream stream = System.IO.File.Create(Path + file.Name);
stream.Write(document, 0, document.Length);
stream.Close();
}
}
});
Error
Couldn't access file even though if I try it again in a moment it works fine.