title: C# Downloading Image with same name as in URL
body: <p>I've got a method to save an image from requested website url, but it saves image as <code>wallpaper.jpg</code>. Is there a possiblity to save an image with the same name as in given url (for example <a href="https://i.imgur.com/l7s8uDA.png" rel="nofollow">https://i.imgur.com/l7s8uDA.png</a> as a <code>l7s8uDA.jpg</code>?</p>
  
  <p>Here's the code:</p>
  
  <pre><code>private void DownloadImage(string uri)
  {
      string fileName = Environment.CurrentDirectory + "\\Wallpapers\\wallpaper.jpg";
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
      HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  
      // Check that the remote file was found. The ContentType
      // check is performed since a request for a non-existent
      // image file might be redirected to a 404-page, which would
      // yield the StatusCode "OK", even though the image was not
      // found.
      if ((response.StatusCode == HttpStatusCode.OK ||
          response.StatusCode == HttpStatusCode.Moved ||
          response.StatusCode == HttpStatusCode.Redirect) &amp;&amp;
          response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase))
      {
          // if the remote file was found, download oit
          using (Stream inputStream = response.GetResponseStream())
          using (Stream outputStream = File.OpenWrite(fileName))
          {
              byte[] buffer = new byte[4096];
              int bytesRead;
  
              do
              {
                  bytesRead = inputStream.Read(buffer, 0, buffer.Length);
                  outputStream.Write(buffer, 0, bytesRead);
              } while (bytesRead != 0);
          }
      }
  }
  </code></pre>