2012-07-07 55 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(); 

重命名其中的一个,以避免混乱。那么我认为你将能够找出解决问题的正确方法。