将文件保存到文件夹

问题描述:

这是我写的代码,但它不起作用。我不知道如何解决它。我希望你能帮助我。我出出主意:(你是我唯一的希望。将文件保存到文件夹

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

请解释它不起作用的方式。不编译?你有错误信息吗?等 – 2012-02-15 22:42:03

+0

名称'目的地'不存在于当前上下文 – docxx 2012-02-15 22:43:32

+0

然后声明它 – 2012-02-15 22:47:08

它看起来像您使用的是所谓的目标变量在这条线

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

但是你还没有宣布,可变的,并且它是buttonStart_Click方法中的一个值

您在buttonSaveTo_Click方法中声明了一个名为destination的变量,如果这是您在buttonStart_Click中要使用的值,则需要将其设置为类级别字段,以便在其旁边声明它'布旗'

e.g:

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

并从

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

字符串声明您使用destination这里:

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

但它是在另一个范围(功能)声明。具体而言,它在buttonSaveTo_Click()中声明。

int downloadedNumber = 0; 
string destination; 

那么,在年底改线:

你可以在顶部像这样int downloadedNumber = 0;下宣布它

destination = fbd.SelectedPath; 

我不能保证,这将完全解决您的代码,但它会使destination在您尝试使用它的两个地方可用。