4

我是新手,所以我敢肯定这是我所缺少的非常基本的东西。

我有一个简单的程序来运行一个csv包含图像链接的文件,以将这些图像保存在指定的保存文件位置。

我正在将包含 的单元格解析urlList<string[]>.

如果我把GetImage(@"http://www.example.com/picture.jpg", 1)我的GetImage功能按它应该的方式执行。当我尝试使用循环并传入str[0]变量时,我收到有关路径中非法字符的错误。

我用 aMessageBox告诉我有什么区别,据我所知,当我将它传递给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);                         
    }
4

1 回答 1

2

无论是哪个函数或方法创建了这些引号,您都可以将它们全部替换。

String myUrl = str[0];
myUrl = myUrl.Replace("\"", "");
GetImage(myUrl, str[4]);

我认为您的文件包含引号或 parseCSV 方法创建它们。

更新:

我使用了这段代码,它完全没有问题并且没有引号:

static void Main(string[] args)
{
  string fileName = "Test";
  //folderBrowserDialog1.ShowDialog();
  string saveLocation = ".\\";
  //textBox2.Text = saveLocation;
  List<string[]> file = new List<string[]>
  {
    new string[] { "http://www.example.com", "1", "1", "1", "1"},
    new string[] { "http://www.example.com", "2", "2", "2", "2"},
  };
  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 static void GetImage(string url, string prodID)
{
  string saveLocation = ".\\"; // textBox2.Text + @"\"; ;
  saveLocation += prodID + ".jpg";
  WebClient webClt = new WebClient();
  Console.WriteLine(url);
  webClt.DownloadFile(url, saveLocation);
}
于 2012-10-14T14:11:59.447 回答