我是新手,所以我确定我错过了一些基本的东西。
我有一个简单的程序来运行一个包含图像链接的csv
文件,将这些图像保存在指定的保存文件位置中。
我正在将包含url
的单元格解析为List<string[]>
。
如果我输入 GetImage(@"http://www.example.com/picture.jpg", 1)
,我的GetImage
函数会按照预期执行。但当我尝试使用循环并传递str [0]
变量时,我会收到关于路径中非法字符的错误。
我已经使用MessageBox
告诉我区别所在,据我所知,当我将str[0]
传递给函数时,它会添加双引号(例如,"http://www.example.com"被显示,而不是http://www.example.com,就像我只发送一个字符串时一样)。
我做错了什么?
private void button2_Click(object sender, EventArgs e)
{
string fileName = textBox1.Text;
folderBrowserDialog1.ShowDialog();
string saveLocation = folderBrowserDialog1.SelectedPath;
textBox2.Text = saveLocation;
List<string[]> file = parseCSV(fileName);
int count = 0;
foreach (string[] str in file)
{
if (count != 0)
{
GetImage(str[0], str[4]);
}
count++;
}
//GetImage(@"http://www.example.com/picture.jpg", "1");
}
private void GetImage(string url, string prodID)
{
string saveLocation = textBox2.Text + @"\";;
saveLocation += prodID + ".jpg";
WebClient webClt = new WebClient();
webClt.DownloadFile(url, saveLocation);
}
saveLocation
变量上?最好使用Path.Combine()
来处理路径(比使用textBox2.Text + @"\" + prodID + ".jpg";
更好)。你能检查一下 saveLocation 的值吗? - NasreddinewebClt.DownloadFile(new Uri(url), saveLocation);
,似乎部分输入不是有效的 URI。 - CandideList<string[]> file
内容的示例? - Nasreddine