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