公司禁用用迅雷,禁用所有P2P协议的软件,自己写个断点续传的工具

  1. 云栖社区>
  2. 博客>
  3. 正文

公司禁用用迅雷,禁用所有P2P协议的软件,自己写个断点续传的工具

xland 2009-10-27 19:09:00 浏览424
展开阅读全文

目前仅支持HTTP协议

下一步的工作是支持FTP协议

再下一步的工作是对原始URL进行智能分析

发此文以抗议像吉日兄这样的总监或者老板

 

img_1c53668bcee393edac0d7b3b3daff1ae.gifimg_405b18b4b6584ae338e0f6ecaf736533.gifCode
    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, 0512);                
                
while (nReadSize > 0)
                {
                    fs.Write(nbytes, 
0, nReadSize);
                    nReadSize 
= ns.Read(nbytes, 0512);
                }
                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(thisstring.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");
        }

网友评论

登录后评论
0/500
评论
xland
+ 关注