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”。没有文件夹不是只读的。无论我在哪里设置保存位置,它都会给我同样的错误。

4

2 回答 2

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;
于 2012-07-07T22:31:28.470 回答
4

问题是您已经声明了两个具有相同名称的变量 - fbd

private static string fbd = String.Empty;

FolderBrowserDialog fbd = new FolderBrowserDialog();

重命名其中之一以避免混淆。然后我认为您将能够找出解决问题的正确方法。

于 2012-07-07T22:32:56.500 回答