C#百度网盘搜索工具附源码
本人C#新手,这个软件是半成品,仅供大家学习交流之用。Release版本下载:
链接:http://pan.baidu.com/s/1qW8nxbY 密码:5jin
本以为正则匹配html文本是件非常简单的事情,所以一时心血来潮想写个百度网盘搜索工具来练练手,结果折腾了两天,软件没写好问题倒一大堆,最大的问题就是ListView控件加载资源速度太慢,希望能得到吾爱网友的指导~
C#源码下载(我用的是VS2012,打不开的可以直接看以下代码)
附件地址:
百度网盘地址:链接:http://pan.baidu.com/s/1sjPMsJr 密码:oco8
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
/*本人C#新手,这个软件是半成品,仅供大家学习交流之用*/
namespace BaiDuYun
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class ListViewNF : System.Windows.Forms.ListView//此类是为了防止ListView控件加载数据时闪烁
{
public ListViewNF()
{
// 开启双缓冲
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
// Enable the OnNotifyMessage event so we get a chance to filter out
// Windows messages before they get to the form's WndProc
this.SetStyle(ControlStyles.EnableNotifyMessage, true);
}
protected override void OnNotifyMessage(Message m)
{
//Filter out the WM_ERASEBKGND message
if (m.Msg != 0x14)
{
base.OnNotifyMessage(m);
}
}
}
public struct strPCFileinfo
{
public string strName;
public string strSize;
public string strOwner;
public string strTime;
public string strCount;
public string strRemark;
public string strDownloadUrl;
};
List<strPCFileinfo> file = new List<strPCFileinfo>();//这个应该可以替换下面的fileinfo数组,本版本未用
public static object locker = new object();//添加一个对象作为锁
private delegate void SearchResultCallBack(int index,strPCFileinfo file);
private SearchResultCallBack searchResultCallBack;
private void SearchResultMethod(int index,strPCFileinfo file)//往listview控件添加信息
{
ListViewItem firstrecord = new ListViewItem(index.ToString());
firstrecord.SubItems.Add(file.strName);
firstrecord.SubItems.Add(file.strSize);
firstrecord.SubItems.Add(file.strOwner);
firstrecord.SubItems.Add(file.strTime);
firstrecord.SubItems.Add(file.strCount);
firstrecord.SubItems.Add(file.strRemark);
listView1.Items.Add(firstrecord);
}
int index;
strPCFileinfo[] fileinfo;
string htmlCompare=null;
int page;
private bool IsValideMethod(string url)//判断文件是否有效
{
// DateTime start = DateTime.Now;
//url = @"http://pan.baidu.com/share/link?shareid=1407451583&uk=2318901111";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream ReceiveStream = res.GetResponseStream();
Encoding encode = System.Text.Encoding.UTF8;
StreamReader sr = new StreamReader(ReceiveStream, encode);
string strResult = "";
Char[] read = new Char;
int count = sr.Read(read, 0, 512);
while (count > 0)
{
String str = new String(read, 0, count);
strResult += str;
count = sr.Read(read, 0, 256);
}
MatchCollection match3 = Regex.Matches(strResult, "<title>百度云 网盘-链接不存在</title>");
try
{
if (match3.Success)
return false;
else
return true;
}
catch { }
return true;
}
private void RegexMatchResult()//核心代码,正则匹配所有符合要求的资源列表
{
//}\"[\s]*?(href=\"http://.*\")+?[^(文件名)]*立即关注 (匹配文件夹)
//string strPat ="}\"[\\s]*?(href=\"http://.*\")+?[\\s\\S]*?文件名:(.+?) 文件大小:(.+?) 分享者:(.+?) 分享时间:(.+?) 下载次数:(.+?)</div>";
index=page =0;
fileinfo = new strPCFileinfo;
string strPat = "}\"[\\s]*?(href=\"http://.*\")+?[^(立即关注)]*?文件名:(.+?) 文件大小:(.+?) 分享者:(.+?) 分享时间:(.+?) 下载次数:(.+?)</div>";
Regex reg = new Regex(strPat);
WebClient client = new WebClient();
client.Proxy = null;
client.Encoding = Encoding.GetEncoding("utf-8");
string strUrl = string.Format(@"http://www.baidu.com/s?wd=site%3Apan.baidu.com%20{0}&pn={1}&ie=utf-8", textBox1.Text, page);
string html = client.DownloadString(strUrl);
bool IsSame = true;
//string html = client.DownloadString(@"http://www.baidu.com/s?wd=site%3Apan.baidu.com%20易语言&pn=10&ie=utf-8");
lock (locker)//锁
{
strPCFileinfo[] fileinfocompare = new strPCFileinfo;
while (IsSame && (page <= 2000))
{
MatchCollection matches = Regex.Matches(html, strPat);
//Trace.WriteLine("hello");
//fileinfo = new strPCFileinfo;
for (int i = 0; i < matches.Count; i++)
{
if (matches.Success)
{
//Trace.WriteLine(matches.Value);
int j = index;
index++;
string strMatch = matches.Value;
strMatch = strMatch.Replace("<em>", "");
strMatch = strMatch.Replace("</em>", "");
strMatch = strMatch.Replace("</div>", "");
strMatch = strMatch.Replace(" ", "");
//strMatch = strMatch.Replace("\n", "");
//Regex reg1 = new Regex("+://[^\\s]*");
MatchCollection match = Regex.Matches(strMatch, "+://[^\\s]*");
fileinfo.strDownloadUrl = match.Value.Substring(0, match.Value.IndexOf("\""));
fileinfo.strName = strMatch.Substring(strMatch.IndexOf("文件名") + 4);
fileinfo.strName = fileinfo.strName.Substring(0, fileinfo.strName.IndexOf("文件大小"));
fileinfo.strSize = strMatch.Substring(strMatch.IndexOf("文件大小") + 5);
fileinfo.strSize = fileinfo.strSize.Substring(0, fileinfo.strSize.IndexOf("分享者"));
fileinfo.strOwner = strMatch.Substring(strMatch.IndexOf("分享者") + 4);
fileinfo.strOwner = fileinfo.strOwner.Substring(0, fileinfo.strOwner.IndexOf("分享时间"));
fileinfo.strTime = strMatch.Substring(strMatch.IndexOf("分享时间") + 5);
fileinfo.strTime = fileinfo.strTime.Substring(0, fileinfo.strTime.IndexOf("下载次数"));
fileinfo.strCount = strMatch.Substring(strMatch.IndexOf("下载次数") + 5);
MatchCollection match2 = Regex.Matches(fileinfo.strCount, "+");
try
{
if (match2.Success)
{
fileinfo.strCount = match2.Value;
}
else
fileinfo.strCount = "...";
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message);
}
if (fileinfo.strSize == "-")
{
fileinfo.strRemark = "文件夹";
}
else
{
if(IsValideMethod(fileinfo.strDownloadUrl)==false)
fileinfo.strRemark = "该资源已失效";
else
fileinfo.strRemark = " ";
}
if (fileinfocompare.strOwner == fileinfo.strOwner && fileinfocompare.strTime == fileinfo.strTime)
{
IsSame = false;
}
else
{
fileinfocompare = fileinfo;
//fileinfo.strSize = fileinfo.strSize.Substring(0, fileinfo.strSize.IndexOf("分享者"));
listView1.Invoke(searchResultCallBack, index, fileinfo);
//Trace.WriteLine(strMatch);
}
}
}
htmlCompare = html;
page = page + 10;
string strUrl1 = string.Format(@"http://www.baidu.com/s?wd=site%3Apan.baidu.com%20{0}&pn={1}&ie=utf-8", textBox1.Text, page);
html = client.DownloadString(strUrl1);
}
} //锁
}
private void button1_Click(object sender, EventArgs e)//单击搜索按钮触发事件
{
listView1.Items.Clear();
//RegexMatch();
searchResultCallBack = new SearchResultCallBack(SearchResultMethod);
Thread[] SearchResultThread = new Thread;
for (int p = 0; p < 5; p++)
{
SearchResultThread = new Thread(RegexMatchResult);
SearchResultThread.Start();
}
//SearchResultThread.Start();
}
//右击打开资源地址事件
public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
private void 打开资源地址ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listView1.SelectedIndices.Count > 0)
{
// 转到下载页ToolStripMenuItem.Visible = true;
int index = listView1.SelectedIndices;//选中第index+1行
ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder(fileinfo.strDownloadUrl), new StringBuilder(""), new StringBuilder(""), 1);
}
}
}
}
原来是这样实现的啊 谢谢楼主开源分享 感谢分享,下载试一下 非常感谢楼主的分享,学习了,谢谢 认真学习中。。。。。。厉害楼主 感谢分享呀。最近也打算学习c# 感觉C#写的东西太鸡肋,,还不如VC写 看看效果怎么样 {:1_914:}
多谢楼主分享哟 顶楼主一个