目前仅支持HTTP协议
下一步的工作是支持FTP协议
再下一步的工作是对原始URL进行智能分析
发此文以抗议像吉日兄这样的总监或者老板
Code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void DownLoad()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("D:\\中转\\data.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("data").ChildNodes;
XmlElement xe = (XmlElement)nodeList.Item(nodeList.Count - 1);
if (xe.GetAttribute("is_finish").Equals("true"))
{
MessageBox.Show("none task in the list");
return;
}
string StrFileName = "D:\\中转\\" + xe.GetAttribute("name");
string StrUrl = xe.GetAttribute("address");
long lStartPos = 0;
System.IO.FileStream fs;
if (System.IO.File.Exists(StrFileName))
{
fs = System.IO.File.OpenWrite(StrFileName);
lStartPos = fs.Length;
fs.Seek(lStartPos, System.IO.SeekOrigin.Current); //移动文件流中的当前指针
}
else
{
fs = new System.IO.FileStream(StrFileName, System.IO.FileMode.Create);
lStartPos = 0;
}
//打开网络连接
try
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(StrUrl);
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();
EditNode();
MessageBox.Show("Down Ok!");
}
catch (Exception ex)
{
fs.Close();
MessageBox.Show("Down Error!");
}
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("D:\\中转\\data.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("data").ChildNodes;
XmlElement xe = (XmlElement)nodeList.Item(nodeList.Count - 1);
string StrFileName = "D:\\中转\\" + xe.GetAttribute("name");
FileInfo fi = new FileInfo(StrFileName);
DialogResult dr = MessageBox.Show(this, string.Format("already:{0}kb\nOK:exit\ncancel:continue", fi.Length / 1024), "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
if (!dr.Equals(DialogResult.Cancel))
{
this.Close();
this.Dispose();
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
if (!string.IsNullOrEmpty(textBox1.Text.Trim())&&!string.IsNullOrEmpty(textBox2.Text.Trim()))
{
InsertNode();
}
System.Threading.Thread obj = new System.Threading.Thread(new System.Threading.ThreadStart(this.DownLoad));
//设置为前台线程,即使主方法执行结束了我的线程仍在执行
obj.IsBackground = true;
obj.Start();
}
private void EditNode()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("D:\\中转\\data.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("data").ChildNodes;
XmlElement xe = (XmlElement)nodeList.Item(nodeList.Count - 1);
xe.SetAttribute("is_finish", "true");
xmlDoc.Save("D:\\中转\\data.xml");//保存。
}
private void InsertNode()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("D:\\中转\\data.xml");
XmlNode root = xmlDoc.SelectSingleNode("data");
XmlElement xe1 = xmlDoc.CreateElement("file");
xe1.SetAttribute("name", textBox1.Text.Trim());
xe1.SetAttribute("address", textBox2.Text.Trim());
xe1.SetAttribute("is_finish", "false");
root.AppendChild(xe1);
xmlDoc.Save("D:\\中转\\data.xml");
}