【问题标题】:'string' does not contain a definition for/“字符串”不包含/的定义
【发布时间】:2012-07-07 21:55:54
【问题描述】:

错误: “string”不包含“SelectedPath”的定义,并且找不到接受“string”类型的第一个参数的扩展方法“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”。 没有文件夹不是只读的。无论我在哪里设置保存位置,它都会给我同样的错误。

【问题讨论】:

  • 哦,伙计,我只是从您的代码中查看了您的一张图片,并希望我没有,尽管我笑了!
  • @MattRoberts LOL 是第一个吗?
  • @MattRoberts 担心检查是否是 nsfw xD

标签: c#


【解决方案1】:

fdb 是一个字符串。您已经声明了一个名为 fdb 的字符串类型的类范围字段,它没有属性 SelectedPath

在你的 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;

【讨论】:

  • 啊,我明白了,我有私有静态字符串 fbd = String.Empty;所以我可以使用 button3_click 中的变量,这对 C# 来说还是新手,现在只是想弄清楚为什么我所有的保存位置都被拒绝评估
【解决方案2】:

问题是你声明了两个同名的变量 - fbd:

private static string fbd = String.Empty;

FolderBrowserDialog fbd = new FolderBrowserDialog();

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多