title: c# foreach loop for downloading image just works once
body: <p>I am creating a pc window application that downloads image from a website url. The user is allowed to select which folder to download to. </p>
  
  <p>But the problem I have is that the foreach loop only does this process once...</p>
  
  <pre><code>public bool Url_checker(string link)
  {
      try
      {
          //Creating the HttpWebRequest
          HttpWebRequest request = WebRequest.Create(link.Trim()) as HttpWebRequest;
          //Setting the Request method HEAD, you can also use GET too.
          request.Method = "HEAD";
          //Getting the Web Response.
          HttpWebResponse response = request.GetResponse() as HttpWebResponse;
          //Returns TRUE if the Status code == 200
          return (response.StatusCode == HttpStatusCode.OK);
      }
      catch (WebException)
      {
          return false;
      }
  }
  
  private void submit_Click(object sender, EventArgs e)
  {
      FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
      folderBrowserDialog1.ShowDialog();
      string saveToThisFolder = folderBrowserDialog1.SelectedPath ;
      int i = 0;
      var di = new DirectoryInfo(saveToThisFolder);
      di.Attributes &amp;= ~FileAttributes.ReadOnly;
  
      int counter = 0;
         foreach (string x in urllist.Lines)
         {
             string EndOfURL = x.Substring(x.Length - 4);
             if (Url_checker(x) &amp;&amp; (EndOfURL == ".jpg" || EndOfURL == ".gif" || EndOfURL == ".jpeg" || EndOfURL == ".png"))
             {
                byte[] data;
               //using (WebClient client = new WebClient())
                //{
                WebClient client = new WebClient();
                    data = client.DownloadData(x);
               // }
  
                File.WriteAllBytes(saveToThisFolder + @"\" + (i++ + EndOfURL), data);
                counter++;
                workingURL.Text += x; //+ System.Environment.NewLine
             }
            else
            {
  
                errorURL.Text += (x + System.Environment.NewLine);
            }
         }
  
  
      folderBrowserDialog1.Dispose();
  }
  </code></pre>
  
  <p>GUI design is here. </p>
  
  <p><img src="https://i.stack.imgur.com/QYIZR.png" alt="enter image description here"></p>
  
  <p>The last result is this, </p>
  
  <p><img src="https://i.stack.imgur.com/0DXYB.png" alt="enter image description here"></p>
  
  <p>It only prints and downloads once. Its as if, its only reading the 1st line of the textbox. Or The foreach loop is wrong.</p>
  
  <p>Thanks </p>
  
  <p><strong>Here is the new CODE, CLeaned</strong>
   private void submit_Click(object sender, EventArgs e)
          {</p>
  
  <pre><code>        FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
          folderBrowserDialog1.ShowDialog();
          string saveToThisFolder = folderBrowserDialog1.SelectedPath;
          int i = 0;
          var di = new DirectoryInfo(saveToThisFolder);
          di.Attributes &amp;= ~FileAttributes.ReadOnly;
  
          int counter = 0;
          foreach (string x in urllist.Lines)
          {
              try
              {
                  string EndOfURL = x.Substring(x.Length - 4);
                  if (Url_checker(x) &amp;&amp; (EndOfURL == ".jpg" || EndOfURL == ".gif" || EndOfURL == ".jpeg" || EndOfURL == ".png"))
                  {
  
                      byte[] data;
                      using (WebClient client = new WebClient())
                      {                            
                        data = client.DownloadData(x);
                        File.WriteAllBytes(saveToThisFolder + @"\" + ("MotivatinalQuoteImage" + (i++) + EndOfURL), data);
                        counter++;
                        workingURL.Text += x + System.Environment.NewLine;
                      }
                  }
                  else
                  {
                      errorURL.Text += (x + System.Environment.NewLine);
                  }
              }
              catch (Exception ex)
              {
                  errorURL.Text += ex;
              }
  
          }
          folderBrowserDialog1.Dispose();
      }
  </code></pre>
  
  <p><strong><em>So after debugging, I FOUND OUT THAT the mistake is SOMEWHERE between</em></strong></p>
  
  <pre><code>                       byte[] data;
                         using (WebClient client = new WebClient())
                         {
                             data = client.DownloadData(x);
                             File.WriteAllBytes(saveToThisFolder + @"\" + ("MotivatinalQuoteImage" + (i++) + EndOfURL), data);
                             counter++;
                             workingURL.Text += x + System.Environment.NewLine;
                         } 
  </code></pre>
  
  <p>Here is what happened when I waited. After it only downloaded 1 image.
  <img src="https://i.stack.imgur.com/zJ392.png" alt="http://i.imgur.com/eBtOW1w.png"></p>