错误: 'string'不包含'SelectedPath'的定义,并且没有扩展方法'SelectedPath'接受类型'string'的第一个参数可以找到
代码:
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'被拒绝。 并且该文件夹不是只读的。无论我在哪里设置保存位置,它都会给我同样的错误。
答案 0 :(得分:5)
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;
答案 1 :(得分:4)
问题是您已声明了两个名称相同的变量 - fbd
:
private static string fbd = String.Empty;
FolderBrowserDialog fbd = new FolderBrowserDialog();
重命名其中一个以避免混淆。那么我认为你将能够找到解决问题的正确方法。