Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1283051
  • 博文数量: 135
  • 博客积分: 10588
  • 博客等级: 上将
  • 技术积分: 1325
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-18 11:12
文章分类

全部博文(135)

文章存档

2013年(6)

2012年(3)

2011年(11)

2010年(7)

2009年(14)

2008年(6)

2007年(42)

2006年(46)

分类:

2007-01-07 16:12:28

blog不能备份怎么办,自己写一个。
基本原理是先根据输入的rss地址,获取rss文件,
private void downRss(string url)
        {
            txtContent.Text = "正在下载...";
            this.Refresh();
            try
            {
                WebClient MyWebClient = new WebClient();

                MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。               
                MyWebClient.DownloadFile(url, "output.xml");
            }
            catch (WebException webEx)
            {
                MessageBox.Show(webEx.Message.ToString());
            }

            txtContent.Text = "下载完成,正在读取文件...";
            this.Refresh();

            StreamReader sr = new StreamReader("output.xml");
            string strcontent = "";
            while (!sr.EndOfStream)
            {
                strcontent += sr.ReadLine();
            }

            sr.Close();
            txtContent.Text = strcontent;
        }解析从中取出每篇文章的标题和链接,可以显示在一个listView控件中,
private void xmlToLstView(string filename)
        {
            listView1.Items.Clear();
            //updateTreeView(txtUrl.Text);
            //updateTreeView("output.xml");
            XmlDocument tmpxmldoc = new XmlDocument(); //实例化XML文档
            tmpxmldoc.Load(filename); //加载XML文档
            XmlNodeList nodeList;
            nodeList = tmpxmldoc.SelectNodes("/rss/channel/item");
            try
            {
                for (int i = 0; i < nodeList.Count; i++)
                {
                    ListViewItem item = new ListViewItem(new string[]{
                                        nodeList[i].SelectSingleNode("title").InnerText,
                                        nodeList[i].SelectSingleNode("link").InnerText,
                                        nodeList[i].SelectSingleNode("pubDate").InnerText}
                                    );
                    listView1.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

然后根据listview中选中的项链接下载此文件。
private void downSelectPage()
        {
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (this.listView1.Items[i].Checked)
                {
                    DirectoryInfo dir = new DirectoryInfo("Down");
                    if (!dir.Exists)
                    {
                        Directory.CreateDirectory("Down");//如果不存在,则创建子文件夹
                    }
                    string filename = Directory.GetCurrentDirectory() + "\\Down\\" + this.listView1.Items[i].SubItems[0].Text + ".html";
                    string url = this.listView1.Items[i].SubItems[1].Text;

                    try
                    {


                        txtContent.Text = "正在下载" + filename + "文件...";
                        this.Refresh();

                        WebClient MyWebClient = new WebClient();
                        MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。               
                        Byte[] pageData = MyWebClient.DownloadData(url);//从指定网站下载数据
                        string pageHtml = Encoding.Default.GetString(pageData);

                        FileStream fs = new FileStream(filename, FileMode.Create);
                        fs.Write(pageData, 0, pageData.Length);
                        fs.Close();

                        txtContent.Text = filename + "下载完成!";
                        this.Refresh();
                    }

                    catch (WebException webEx)
                    {
                        MessageBox.Show(webEx.Message.ToString());
                    }


                }

            }
        }
还有很多毛病,比如速度慢,不能下载其中的图片,不过暂时解决了blog备份问题。
源代码在此,可以改进的余地很大。
文件:blogDown.rar
大小:190KB
下载:下载
阅读(2575) | 评论(3) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-04-11 12:12:20

355628

chinaunix网友2010-04-11 12:10:16

123

chinaunix网友2009-03-24 16:58:18

谢谢分享