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 |
下载: | 下载 |
|
阅读(2571) | 评论(3) | 转发(0) |