title: How to programmatically download a large file in C#
body: <p>I need to programmatically download a large file before processing it. What's the best way to do that? As the file is large, I want to specific time to wait so that I can forcefully exit. </p>
  
  <p>I know of WebClient.DownloadFile(). But there does not seem a way to specific an amount of time to wait so as to forcefully exit. </p>
  
  <pre><code>try
  {
      WebClient client = new WebClient();
      Uri uri = new Uri(inputFileUrl);
      client.DownloadFile(uri, outputFile);
  }
  catch (Exception ex)
  {
      throw;
  }
  </code></pre>
  
  <p>Another way is to use a command line utility (wget) to download the file and fire the command using ProcessStartInfo and use Process' WaitForExit(int ms) to forcefully exit. </p>
  
  <pre><code>ProcessStartInfo startInfo = new ProcessStartInfo();
  //set startInfo object
  
  try
  {
      using (Process exeProcess = Process.Start(startInfo))
      {
          //wait for time specified
          exeProcess.WaitForExit(1000 * 60 * 60);//wait till 1m
  
          //check if process has exited
          if (!exeProcess.HasExited)
          {
              //kill process and throw ex
              exeProcess.Kill();
              throw new ApplicationException("Downloading timed out");
          }
      }
  }
  catch (Exception ex)
  {
      throw;
  }
  </code></pre>
  
  <p>Is there a better way? Please help. Thanks.</p>