简体   繁体   中英

Move a file from a SharePoint library to a UNC path

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.

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('/', '\\');
    }

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM