title: Download excel files from sharepoint site to local system using C#
body: <p>I have a requirement of downloading an excel file from sharepoint to my local system.
  I have written the below method.</p>
  
  <pre><code>    internal static void DownloadFilesFromSharePoint(string siteUrl, string folderPath, string tempLocation)
      {
          ClientContext ctx = new ClientContext(siteUrl);
          ctx.Credentials = new NetworkCredential("username", "password");
  
          FileCollection files = ctx.Web.GetFolderByServerRelativeUrl(folderPath).Files;
  
          ctx.Load(files);
          ctx.ExecuteQuery();----FAILED HERE
  
          foreach (File file in files)
          {
              FileInformation fileInfo = File.OpenBinaryDirect(ctx, file.ServerRelativeUrl);
              ctx.ExecuteQuery();
  
              var filePath = tempLocation + file.Name;
              using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
              {
                  fileInfo.Stream.CopyTo(fileStream);
              }
          }
      }
  </code></pre>
  
  <p>To call the above method i have the following in Main method:</p>
  
  <pre><code>    public static void Main()
      {
      Program.DownloadFilesFromSharePoint("http://sp.frk.com/teams/ResearchSystemsSupport/GT%20Resources/Forms/AllItems.aspx", "/TeamDocuments", @"c:\");
  
      }
  </code></pre>
  
  <p>But the code above is not working.
  I have marked the point where the code gave an exception(by marking----FAILED HERE) while debugging. Can anybody point out the problem here.
  Can anybody help me on this.Please give me a detailed explanation as well if you could.</p>