客户端=新的WebClient();是否导致NullReference异常?

时间:2018-09-10 18:41:24

标签: c# webclient nullreferenceexception

所以这段代码昨晚起作用了,我醒来并再次开始处理它,突然之间,它在下一行抛出了NullReference异常

client = new WebClient();

这是该方法的完整代码

//Loop through all image URL's and download files.
int fileIndex = 0;
WebClient client;
foreach (string url in newProduct.aliImageURLs)
{
    //Replace quotes with empty chars.
    string formattedURL = url.Replace("\"", "");
    Console.WriteLine(formattedURL);

    //Get file extension
    string extension = Path.GetExtension(formattedURL);

    //Save local path
    newProduct.localImagePaths[fileIndex] = "c:\\SSBDB\\Pictures\\" + newProduct.AliName + "\\" + fileIndex + extension;

    //Set up Web Client
    client = new WebClient();

    //Download Files and increase index
    client.DownloadFileAsync(new Uri(formattedURL), @"c:\SSBDB\Pictures\" + fileIndex + extension);
    fileIndex++;
}

0 个答案:

没有答案