Download a WebFile

Oct 20, 2011 at 7:36 PM

I am confused on how to download a File from SkyDrive using the DownloadWebFile() Function.

Any help will be greatfull

Nov 3, 2011 at 7:51 PM
Edited Nov 3, 2011 at 8:01 PM

Assuming that you have some root folders and that there are some files inside these folders:

SkyDriveServiceClient skyDriveClient = new SkyDriveServiceClient();

skyDriveClient.LogOn("user", "pass"); //replace it with your user and pass.

WebFolderInfo[] rootWebFolders = skyDriveClient.ListRootWebFolders();
foreach (WebFolderInfo rootWebFolder in rootWebFolders)
{

      WebFileInfo[] files = skyDriveClient.ListSubWebFiles(rootWebFolder);

      foreach(WebFileInfo fi in files)

           Stream fileStream = skyDriveClient.DownloadWebFile(fi);  

}

Regards,

Alexander Yefimochkin

Jul 24, 2012 at 9:31 AM

I also found it's a stream :

         private bool InternalDownloadWebFile(WebFileInfo webFile, string dstFolderPath, bool overwrite = true)
        {
            if (webFile == null) return false;
            if (!isLogon) throw new Exception("You must Login first!");

            FileStream fs = null;
            string dstFilePath = Path.Combine(dstFolderPath, webFile.Name);

            try
            {
                if (File.Exists(dstFilePath))
                {
                    if (!overwrite) throw new IOException("Destination File alread exist!");
                    File.Delete(dstFilePath);
                }

                if (!Directory.Exists(dstFolderPath)) Directory.CreateDirectory(dstFolderPath);
                fs = File.Open(dstFilePath, FileMode.OpenOrCreate);
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to create destination File", ex);
            }

            Stream input = DownloadWebFile(webFile);


            int chunk = 1; //1 byte , you can set it more than 1 byte , however , when do that , we will failed to open an zip file
            byte[] buffer = new byte[chunk];
            int count = 0; //If there are nothing to read, we will return.
            do
            {
                count = input.Read(buffer, 0, chunk);
                if (count <= 0) break;
                fs.Write(buffer, 0, chunk);
            } while (input.CanRead);
          
            fs.Flush();
            fs.Close();
            return true;
        }