using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Xml;
namespace WindowsFormsApplication1
{
public partial class main : Form
{
public string fileName;
public string fileType;
public string fileUrl;
public string rName;
public string rUrl;
public string rDesc;
public string typeRar = "rar";
public main()
{
InitializeComponent();
main_readXml("http://[url]/file.xml");
this.Text = rName;
}
protected override void OnLoad(EventArgs e)
{
}
private void main_Load(object sender, EventArgs e)
{
main_Download();
}
public void main_Download()
{
if (!System.IO.File.Exists(fileName))
{
WebRequest req = WebRequest.Create(fileUrl);
WebResponse response = req.GetResponse();
Stream stream = response.GetResponseStream();
//Download in chuncks
byte[] buffer
= new byte[1024];
//Get Total Size
int dataLength = (int)response.ContentLength;
//With the total data we can set up our progress indicators
progressBar1.Maximum = dataLength;
//lbProgress.Text = "0/" + dataLength.ToString();
//this.Text = "Downloading...";
Application.DoEvents();
//Download to memory
//Note: adjust the streams here to download directly to the hard drive
FileStream memStream
= new FileStream
(fileName, FileMode
.Create);
while (true)
{
//Try to read the data
int bytesRead = stream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
//Finished downloading
progressBar1.Value = progressBar1.Maximum;
// lbProgress.Text = dataLength.ToString() + "/" + dataLength.ToString();
Application.DoEvents();
break;
}
else
{
//Write the downloaded data
memStream.Write(buffer, 0, bytesRead);
//Update the progress bar
if (progressBar1.Value + bytesRead <= progressBar1.Maximum)
{
progressBar1.Value += bytesRead;
// lbProgress.Text = progressBar1.Value.ToString() + "/" + dataLength.ToString();
progressBar1.Refresh();
Application.DoEvents();
}
}
}
if (fileType == typeRar)
{
rar.Open(fileName, Unrar.OpenMode.Extract);
rar.DestinationPath = "";
while (rar.ReadHeader())
{
rar.Extract();
}
rar.Close();
}
// Application.Exit();
}
else
{
Application.Exit();
}
}
private void main_readXml(string xml_file)
{
XmlTextReader xml_document
= new XmlTextReader
(xml_file
); XmlDocument xDoc
= new XmlDocument
(); xDoc.Load(xml_document);
XmlNodeList config = xDoc.GetElementsByTagName("config");
XmlNodeList actus = ((XmlElement)config[0]).GetElementsByTagName("actus");
XmlNodeList files = ((XmlElement)actus[0]).GetElementsByTagName("file");
foreach (XmlElement nodo in files)
{
int i = 0;
XmlNodeList sName = nodo.GetElementsByTagName("name");
XmlNodeList sType = nodo.GetElementsByTagName("type");
XmlNodeList sUrl = nodo.GetElementsByTagName("url");
fileName = sName[i].InnerText;
fileType = sType[i].InnerText;
fileUrl = sUrl[i].InnerText;
}
foreach (XmlElement nodo in config)
{
int i = 0;
XmlNodeList pName = nodo.GetElementsByTagName("sName");
XmlNodeList pUrl = nodo.GetElementsByTagName("web");
XmlNodeList pDesc = nodo.GetElementsByTagName("desc");
rName = pName[i].InnerText;
rUrl = pUrl[i].InnerText;
rDesc = pDesc[i].InnerText;
}
}
}
}