gpt4 book ai didi

c# - “字符串”不包含/的定义

转载 作者:太空宇宙 更新时间:2023-11-03 17:55:59 25 4
gpt4 key购买 nike

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

最佳答案

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;

关于c# - “字符串”不包含/的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11379166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com