title: How to Get File using Console application in C#?
body: <p>I am building an C# Console Application for GET file which will automatically download the file when I run the console application.</p>
  
  <p>These are my codes:</p>
  
  <pre><code>using System;
  using System.Diagnostics;
  using System.IO;
  using System.Net.Http;
  using System.Net.Http.Headers;
  using System.Threading.Tasks;
  
  namespace WebAPIConsoleNEW
  {
      class Program
      {
  
  
          static void Main(string[] args)
          {
              RunAsync().Wait();
          }
  
          static async Task RunAsync()
          {
              string bookPath_Pdf = @"D:\VisualStudio\randomfile.pdf";
              string bookPath_xls = @"D:\VisualStudio\randomfile.xls";
              string bookPath_doc = @"D:\VisualStudio\randomfile.docx";
              string bookPath_zip = @"D:\VisualStudio\randomfile.zip";
  
              string format = "pdf";
              string reqBook = format.ToLower() == "pdf" ? bookPath_Pdf : (format.ToLower() == "xls" ? bookPath_xls : (format.ToLower() == "doc" ? bookPath_doc : bookPath_zip));
              string fileName = "sample." + format.ToLower();
  
              try
              {
                  using (var client = new HttpClient())
                  {
                      client.BaseAddress = new Uri("http://localhost:49209/");
                      client.DefaultRequestHeaders.Accept.Clear();
                      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("applicaiton/json"));
  
                      Console.WriteLine("GET");
  
                      //converting Pdf file into bytes array
                      var dataBytes = File.ReadAllBytes(reqBook);
  
                      //adding bytes to memory stream
                      var dataStream = new MemoryStream(dataBytes);
  
                      //send request asynchronously
                      HttpResponseMessage response = await client.GetAsync("api/person");
                      response.Content = new StreamContent(dataStream);
                      response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
                      response.Content.Headers.ContentDisposition.FileName = fileName;
                      response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
  
                      //Check that response was successful or throw exception
                      //response.EnsureSuccessStatusCode();
  
                      //Read response asynchronously and save asynchronously to file
                      if (response.IsSuccessStatusCode)
                      {
                          using (var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:49209/api"))
                          {
                              using (
                              Stream contentStream = await (await client.SendAsync(request)).Content.ReadAsStreamAsync(),
                                  fileStream = new FileStream("D:\\VisualStudio\\randomfile.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
                          {
                                  //copy the content from response to filestream
                                  await response.Content.CopyToAsync(fileStream);
                                  //Console.WriteLine();
                              }   
  
                          }
                      }
  
              }
              catch (HttpRequestException rex)
              {
                  Console.WriteLine(rex.ToString());
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.ToString());
              }
          }
      }
  }
  </code></pre>
  
  <p>When I run another ASP.NET application which is my localhost, it only return the default which is value1 and value2 in the Controller. However, I do not have Controller in C# Console Application. I think I just one step away, I had successfully obtain the file and CopyToAsync the file I wanted to download.</p>
  
  <p>Conclusion:
  I want when user runs the application it would straight download the file in a place (or can I use SaveFileDialog to let user decide where to save the file).
  Please help thanks</p>
  
  <p><strong>Update:</strong></p>
  
  <p>At first, I created a ASP.NET Web Application and Create a PersonController and I run the Project. After that I created a console C# Application and then I want to achieve the result of when user runs the console C# Application it would straight download the file to a specific place.</p>
  
  <p>In the first get I uses api/person, and I convert the file int o bytes array and add the bytes array to memory stream. After that, I don't really know what I'm doing is right or wrong. I saw something like CopyToAsync is working then I tried it and implement it but it won't works. My goal is simple I just want to acheive once I run the C# Console application it would straight download the file from a specific localhost address</p>