0
我试图为我的桌面编写一个随机壁纸下载程序。 代码在第一次下载时工作正常,但在 第二次尝试中挂起并引发异常。我试图介绍客户端,并从新的Web客户端开始。 我也试过没有处置。提前致谢。WebClient在第二次下载尝试时抛出异常
public class ChangeWallpaper
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
public static void Main()
{
Random fileNumber = new Random();
string pathStart = "http://www.starcraft2.com/images/screenshots/ss";
string pathEnd = "-hires.jpg";
while (true) //forever loop
{
string randomFile = fileNumber.Next(1, 126).ToString();
WebClient Client = new WebClient();
//OK FIRST TIME -> THROWS EXCEPTION ON SECOND ATTEMPT!
Client.DownloadFile(pathStart + randomFile + pathEnd, "pic.jpg");
Client.Dispose(); //tried removing
Bitmap bm = new Bitmap(Image.FromFile("pic.jpg"));
bm.Save("pic.bmp", ImageFormat.Bmp);
bm.Dispose(); //tried removing - no help
SystemParametersInfo(20, 0, "pic.bmp", 0x01 | 0x02);
Thread.Sleep(60000); // Sleep for 1 minute
}
}
}
...和例外是? – 2010-01-22 20:31:56
异常的性质是什么? – 2010-01-22 20:32:36
显示异常。如果您删除了SystemParametersInfo调用,您是否也会遇到异常? – nos 2010-01-22 20:33:22