1

エラー: '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」へのアクセスが拒否されました。いいえ、フォルダーは読み取り専用ではありません。保存場所をどこに設定しても、同じエラーが発生します。

4

2 に答える 2

5

fdb は文字列です。プロパティ SelectedPath を持たない、文字列型の fdb というクラス全体のフィールドを宣言しました。

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

問題は、同じ名前の 2 つの変数を宣言したことです - fbd:

private static string fbd = String.Empty;

FolderBrowserDialog fbd = new FolderBrowserDialog();

混乱を避けるために、いずれかの名前を変更します。そうすれば、問題の正しい解決策を見つけることができると思います。

于 2012-07-07T22:32:56.500 に答える