Move a file from a SharePoint library to a UNC path

888 views Asked by At

I have a number of files in a document library in SharePoint and the URL paths to those files. I want to be able to move the files from SharePoint to a UNC path location but File.Exists() and File.Move() doesn't work with URL's.

2

There are 2 answers

0
Matt On

Answering my own question because I wasn't able to find any specific answer to this. I found something called DavWWWRoot that allows you to access files through Windows Explorer.

I created the method that converts my URL's into a form that allows me to use File.Move() on the files in my SharePoint Library.

private string ConvertSharePointURLToUNCPath(string sharePointFileURL)
    {
        return sharePointFileURL.Replace(@"http://mySharePointSite/", @"\\mySharePointSite\DavWWWRoot\").Replace('/', '\\');
    }
0
Graham On

Here's an example that iterates through the objects in an SPFileCollection

string networkShareURN = @"\\yourserver\sharefolder";

SPSite site = new SPSite("yoursitecollection");
SPWeb web = site.OpenWeb("sitename");
SPFolder myfolder = web.GetFolder("Shared Documents");
SPFileCollection myfiles = myfolder.Files;
foreach (SPFile file in myfiles)
{
     Byte[] bfile = file.OpenBinary();
     System.IO.File.WriteAllBytes(networkShareURN + file.Name, bfile);
}