簡體   English   中英

“字符串”不包含/的定義

[英]'string' does not contain a definition for/

錯誤 :“字符串”不包含“ 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”。 並且沒有文件夾不是只讀的。 無論我在哪里保存位置,它都會給我同樣的錯誤。

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

private static string fbd = String.Empty;

FolderBrowserDialog fbd = new FolderBrowserDialog();

重命名其中之一以避免混淆。 然后,我認為您將能夠找到解決問題的正確方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM