2012-02-15 12 views
-4

これは私が書いたコードですが、動作しません。私はそれを修正する方法を知らない。あなたが私を助けることを願っています。私はアイデアを午前:(あなたは私の唯一の希望です。ファイルをフォルダに保存しています

namespace Imgur 
{ 
public partial class Form1 : Form 
{ 
    bool flag = true; 
    int downloadedNumber = 0; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    public void buttonStart_Click(object sender, EventArgs e) 
    { 
     buttonStart.Enabled = false; 
     buttonStop.Enabled = true; 
     if (!flag) 
     { 
      flag = true; 
     } 

     for (int i=0;i<100000 && flag;i++) 
     { 
      WebClient webClient = new WebClient(); 
      string pic1 = rnd_str(5); 
      string pic2 = ".jpg"; 
      string picture = pic1 + pic2; 

      //********** GETTING SIZE OF IMAGE *********** 
      Size sz = GetSize("http://i.imgur.com/" + picture); 
      string imageSize = (sz.Width.ToString() + " " + sz.Height.ToString()); ; 
      //******************************************** 

      if(imageSize != "161 81") 
      { 
       webClient.DownloadFile("http://i.imgur.com/" + picture, destination + picture); 

       richTextBox1.Text += String.Format("Downloaded picture: {0}\r\n", picture); 
       downloadedNumber++; 
       textBoxDownloadedNumber.Text = string.Format("{0}", downloadedNumber); 
      } 
      webClient.Dispose(); 
      Application.DoEvents(); 
      if (i == 999995) 
      { 
       flag = false; 
      } 
     } 
     richTextBox1.Text += "theend\n"; 
     buttonStart.Enabled = true; 
     buttonStop.Enabled = false; 
    } 

    public static Size GetSize(string url) 
    { 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
     request.Method = "GET"; 
     request.Accept = "image/gif"; 
     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
     Stream s = response.GetResponseStream(); 
     Bitmap bmp = new Bitmap(s); 
     Size sz = new Size(bmp.Width, bmp.Height); 
     return sz; 
    } 

    public static string rnd_str(int liczba_liter) 
    { 
     Random r = new Random(); 
     int char_type; 
     string return_string = ""; 
     int i =0; 
     for (i = 0; i < liczba_liter; i++) 
     { 
      if (r.Next(1, 3) == 1) 
      { 
       char_type = r.Next(1, 4); 
       switch (char_type) 
       { 
        case 1: 
         return_string += (char)r.Next(48, 58); // convertion int -> ASCII character; 48-57 are ASCII digits 
         break; 
        case 2: 
         return_string += (char)r.Next(97, 123); // convertion int -> ASCII character; as above but small letters 
         break; 
        case 3: 
         return_string += (char)r.Next(65, 91); // as above; large letters 
         break; 
        default: 
         i -= 1; 
         break;//do not add any letter if no type is allowed 
       } 
      } 
      else 
      { 
       i -= 1; 
       return_string += ""; 
      } 
     } 
     return return_string; 
    } 

    private void buttonStop_Click(object sender, EventArgs e) 
    { 
     flag = false; 
     buttonStart.Enabled = true; 
    } 

    public void buttonSaveTo_Click(object sender, EventArgs e) 
    { 
     FolderBrowserDialog fbd = new FolderBrowserDialog(); 
     fbd.ShowDialog(); 
     richTextBox1.Text = fbd.SelectedPath; 
     string destination = fbd.SelectedPath; 
    } 
} 
} 
+1

どのように動作しないか説明してください。コンパイルしませんか?エラーメッセージが表示されますか?など –

+0

'destination'という名前は現在のコンテキストに存在しません – docxx

+0

それを宣言します –

答えて

2

あなたは変数と割り当てられていることを宣言していない、しかし、あなたは、この行の先

webClient.DownloadFile("http://i.imgur.com/" + picture, destination + picture); 

という変数を使用しているように見えますbuttonStart_Clickメソッド内の値

buttonSaveTo_Clickメソッドで宣言されたdestinationという変数があります。これはbuttonStart_Clickで使用する値であれば、クラスレベルフィールドにする必要があります。 'ブール旗'

例えば:

public partial class Form1 : Form 
{ 
    bool flag = true; 
    int downloadedNumber = 0; 
    string destination; 
} 

とあなたがここにdestination使用

public void buttonSaveTo_Click(object sender, EventArgs e) 
{ 
    FolderBrowserDialog fbd = new FolderBrowserDialog(); 
    fbd.ShowDialog(); 
    richTextBox1.Text = fbd.SelectedPath; 
    destination = fbd.SelectedPath; 
} 
0

から文字列の宣言を削除します。

webClient.DownloadFile("http://i.imgur.com/" + picture, destination + picture); 

をしかし、それは別のスコープ(関数)で宣言されています。具体的には、それはで宣言されています。

あなたはこのような上部にint downloadedNumber = 0;の下でそれを宣言することができます:への最後の行を変更すると

int downloadedNumber = 0; 
string destination; 

:私はこれは完全にあなたのコードを修正することを約束することはできません

destination = fbd.SelectedPath; 

が、それはあなたがそれを使用しようとしている2つの場所で利用可能なdestinationになります。

関連する問題