我想用C#.net做一个动态改换桌面的程序。先从XML中读取图片的位置,然后再下载图片,但是为啥我的只能下载第一张图片呢?代码如下!麻烦给看一下!

knox009009 2009-04-17 08:07:46
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication8
{
class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
string wal,wal_path;
Random ro = new Random();
int iResult;
int iUp = 5000;
int iDown = 1;
iResult = ro.Next(iDown, iUp);
//wal_path = "c:\\myprogram\\" + iResult + ".bmp";

// Program p = new Program();
XmlDocument doc1 = new XmlDocument();
doc1.Load("http://localhost/Best_Advertisement.xml");
XmlNodeList l1 = doc1.SelectNodes("//ads[@id]");
foreach (XmlNode xn in l1)
{
XmlNodeReader xnr = new XmlNodeReader(xn);
while (xnr.Read())
{
// int i = 1;
//System.Threading.Thread.Sleep(5000);
wal = xnr.GetAttribute("wallpaper");
wal_path = "c:\\myprogram\\" + iResult + ".bmp";
downPic(wal,wal_path);
// MessageBox.Show(wal);
// Change_pic(wal);
// i=i+1;
// System.Threading.Thread.Sleep(5000);
}
// Change_pic(wal_path);
// System.Threading.Thread.Sleep(5000);

}
}

public static void downPic(string pic, string savepath)
{


long lStartPos = 0;
System.IO.FileStream fs;
if (System.IO.File.Exists(savepath))
{
fs = System.IO.File.OpenWrite(savepath);
lStartPos = fs.Length;
fs.Seek(lStartPos, System.IO.SeekOrigin.Current); //移动文件流中的当前指针
}
else
{
fs = new System.IO.FileStream(savepath, System.IO.FileMode.Create);
lStartPos = 0;
}

//打开网络连接
try
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(pic);
if (lStartPos > 0)
request.AddRange((int)lStartPos); //设置Range值

//向服务器请求,获得服务器回应数据流
System.IO.Stream ns = request.GetResponse().GetResponseStream();

byte[] nbytes = new byte[512];
int nReadSize = 0;
nReadSize = ns.Read(nbytes, 0, 512);
while (nReadSize > 0)
{
fs.Write(nbytes, 0, nReadSize);
nReadSize = ns.Read(nbytes, 0, 512);
}
fs.Close();
ns.Close();
Console.WriteLine("下载完成");
Console.ReadLine();
}
catch (Exception ex)
{
fs.Close();
Console.WriteLine("下载过程中出现错误:" + ex.ToString());
Console.ReadLine();
}



}


public static void Change_pic(string p)
{
string s;
s = p;

// MessageBox.Show(s);
RegistryKey hklm = Registry.CurrentUser;
RegistryKey controlpanel = hklm.OpenSubKey("Control Panel", true);
RegistryKey desktop = controlpanel.OpenSubKey("Desktop", true);
desktop.SetValue("Wallpaper", s);
API.SystemParametersInfo(20, 1, null, 1);
}
class API
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
}


}


}



...全文
151 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧