2012-07-07 73 views
1

錯誤: 「字符串」不包含關於「SelectedPath」和沒有擴展方法「SelectedPath」接受類型「字符串」的第一個參數的定義可以發現「字符串」不包含定義爲/

代碼

private static string fbd = String.Empty; 
    public void button2_Click(object sender, EventArgs e) 
    { 
     FolderBrowserDialog fbd = new FolderBrowserDialog(); 
     fbd.Description = "Select a Folder to save the images."; 
     if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
      textBox1.Text = fbd.SelectedPath; 
    } 

public void button3_Click(object sender, EventArgs e) 
    { 

     List<string> address = new List<string>(); 
     Random r = new Random(); 
     address.Add("http://d24w6bsrhbeh9d.cloudfront.net/photo/4600000_460s.jpg"); 
     address.Add("http://d24w6bsrhbeh9d.cloudfront.net/photo/4600001_460s.jpg"); 
     address.Add("http://d24w6bsrhbeh9d.cloudfront.net/photo/4600002_460s.jpg"); 
     address.Add("http://d24w6bsrhbeh9d.cloudfront.net/photo/4600003_460s.jpg"); 
     //MessageBox.Show(address[r.Next(0, 4)]); 

     if (comboBox1.Text == "10") 
     { 
      string filename = fbd.SelectedPath; 
      MessageBox.Show(fbd); 

      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(address[r.Next(0, 4)])); 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

      if ((response.StatusCode == HttpStatusCode.OK || 
       response.StatusCode == HttpStatusCode.Moved || 
       response.StatusCode == HttpStatusCode.Redirect) && 
       response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase)) 
      { 

       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); 
        using (WebClient client = new WebClient()) 
        { 
         client.DownloadFile(address[r.Next(0, 25)], filename); 
        } 

       } 
      } 
     } 


    } 

說明: 對於button3_Click即時試圖讓它給你打電話設置保存圖像的保存位置。我已經看遍了整個網絡,我沒有找到一種方法來解決這個問題:/

我也收到錯誤,當我手動輸入保存位置。 訪問路徑'H:\ images'被拒絕。 而且該文件夾不是隻讀的。無論我在哪裏設置保存位置,它都會給我提供相同的錯誤。

+1

哦,男人,我只是看着你的代碼之一,你的代碼,並希望我沒有,雖然我笑了起來! – 2012-07-07 22:38:15

+0

@MattRoberts LOL是第一個嗎? – SimpleVar 2012-07-07 22:41:30

+0

@MattRoberts擔心檢查的情況下,它是nsfw xD – user3564421 2016-01-21 15:30:07

回答

5

fdb是一個字符串。您已經聲明瞭一個名爲fdb的類別爲字符串的類級字段,該字段沒有屬性SelectedPath

在yout button2_click方法中,您有一個可能想要訪問的文件對話框,因此您需要聲明而是在全班範圍內。

private FolderBrowserDialog _fbDlg; 
private static string fbd = String.Empty; // Do you really need this? 
    public void button2_Click(object sender, EventArgs e) 
    { 
     _fbDlg = new FolderBrowserDialog(); 
     _fbDlg.Description = "Select a Folder to save the images."; 
     if (_fbDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
      textBox1.Text = _fbDlg.SelectedPath; 
    } 

public void button3_Click(object sender, EventArgs e) 
    { 

     List<string> address = new List<string>(); 
     Random r = new Random(); 
     address.Add("http://d24w6bsrhbeh9d.cloudfront.net/photo/4600000_460s.jpg"); 
     address.Add("http://d24w6bsrhbeh9d.cloudfront.net/photo/4600001_460s.jpg"); 
     address.Add("http://d24w6bsrhbeh9d.cloudfront.net/photo/4600002_460s.jpg"); 
     address.Add("http://d24w6bsrhbeh9d.cloudfront.net/photo/4600003_460s.jpg"); 
     //MessageBox.Show(address[r.Next(0, 4)]); 

     if (comboBox1.Text == "10") 
     { 
      string filename = _fbDlg.SelectedPath; 
+0

啊我看到,我有私人靜態字符串fbd = String.Empty;所以我可以在button3_click中使用這個變量,它仍然是C#的新增功能,現在只是爲了弄清楚爲什麼我的所有保存位置都被拒絕評估 – Jaminb2030 2012-07-07 22:42:09

4

的問題是,您已聲明兩個變量具有相同的名稱 - fbd

private static string fbd = String.Empty; 

FolderBrowserDialog fbd = new FolderBrowserDialog(); 

重命名其中的一個,以避免混亂。那麼我認爲你將能夠找出解決問題的正確方法。