Jeg har to filer, MainForm:
----------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Q4PickupUpdater
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
//distinationTextBox.Text = "C:\\";
distinationTextBox.Text = "C:\\Programmer\\id Software\\Quake 4";
}
// Private Toolstrip Button Events
private void ChooseDir_Click_1(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
string result = folderBrowserDialog1.SelectedPath;
if (result.Length > 0)
distinationTextBox.Text = result; StartUpdater.Enabled = true;
}
private void CloseMain_Click(object sender, EventArgs e)
{
this.Close();
}
private void StartUpdater_Click(object sender, EventArgs e)
{
try
{
UpdateFiles();
}
catch (Exception ex)
{
MessageBox.Show("Problem executing, error: " + ex.Message);
}
StartUpdater.Enabled = false;
}
private void visitHomepageToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start("IExplore", "
http://q4pickup.com");
}
catch (Exception ex)
{
MessageBox.Show("Could not launch Internet Explorer.",
"An error has occurred",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutDialog myAbout = new AboutDialog();
myAbout.ShowDialog();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void editSourcesToolStripMenuItem_Click(object sender, EventArgs e)
{
SourcesDialog mySourcesDialog = new SourcesDialog();
mySourcesDialog.ShowDialog();
}
// Helper methods
private void UpdateFiles()
{
string fileList = "
http://files.q4pickup.dk/updater/index.xml"; statusLabel.Text = "Updating files ...";
try
{
System.Collections.ArrayList result;
result = FetchFileList.ProcessFileFeed(fileList);
progressBar1.Maximum = result.Count;
progressBar1.Value = 0;
int i = 0;
foreach (Q4PickupUpdater.FileItem currentFileItem in result)
{
currentFileLabel.Text = currentFileItem.File;
string path = distinationTextBox.Text + "\\" + currentFileItem.Dir + "\\" + currentFileItem.File;
string dirpath = distinationTextBox.Text + "\\" + currentFileItem.Dir;
// Check if file already exists, if not then go ahead
if (!System.IO.File.Exists(path))
{
//MessageBox.Show("Download file" + currentFileItem.File + " to dir " + currentFileItem.Dir + " test: " + System.IO.Directory.Exists(currentFileItem.Dir.ToString()));
// Check if directory exists, if not then create
if (!System.IO.Directory.Exists(dirpath))
{
System.IO.Directory.CreateDirectory(dirpath);
MessageBox.Show("Created one folder!");
}
// And finally, download the file.
FileDownloader.DownloadFile(currentFileItem.Dest, path);
//MessageBox.Show(currentFileItem.Dir);
statusLabel.Text = "Downloaded " + i + " file of " + progressBar1.Maximum + " file(s)";
i++;
}
progressBar1.Value++;
}
if (i > 0)
{
statusLabel.Text = i + " file(s) were downloaded.";
}
else
{
statusLabel.Text = "No files were downloaded.";
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
//progressBar1.Value = 0;
currentFileLabel.Text = "Done.";
}
}
}
}
FileDownloader.cs
----------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace Q4PickupUpdater
{
class FileItem
{
private string _file;
private string _dir;
private string _dest;
public string File
{
get { return _file; }
set { _file = value; }
}
public string Dir
{
get { return _dir; }
set { _dir = value; }
}
public string Dest
{
get { return _dest; }
set { _dest = value; }
}
}
class FetchFileList
{
public static System.Collections.ArrayList ProcessFileFeed(string fileListURL)
{
// Begin the WebRequest to the desired RSS Feed
System.Net.WebRequest myRequest = System.Net.WebRequest.Create(fileListURL);
System.Net.WebResponse myResponse = myRequest.GetResponse();
// Convert the RSS Feed into an XML document
System.IO.Stream rssStream = myResponse.GetResponseStream();
System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();
rssDoc.Load(rssStream);
// This uses an XPath expression to get all nodes that fall
// under this path.
System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
// Loop through the Item nodes from the RSS Feed and retrieve
// the Title, Link and Description. Then we'll add it to the
// database (if not already present.)
System.Collections.ArrayList returnArrayList = new System.Collections.ArrayList();
for (int i = 0; i < rssItems.Count; i++)
{
System.Xml.XmlNode rssDetail;
FileItem tempFileItem = new FileItem();
rssDetail = rssItems.Item(i).SelectSingleNode("file");
if (rssDetail != null)
{
tempFileItem.File = rssDetail.InnerText;
}
else
{
tempFileItem.File = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("dir");
if (rssDetail != null)
{
tempFileItem.Dir = rssDetail.InnerText;
}
else
{
tempFileItem.Dir = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("dest");
if (rssDetail != null)
{
tempFileItem.Dest = rssDetail.InnerText;
}
else
{
tempFileItem.Dest = "";
}
returnArrayList.Add(tempFileItem);
}
return returnArrayList;
}
}
class FileDownloader
{
public static void DownloadFile(string FileURL, string LocalDestination)
{
// TODO: Need to add some error handlers, like when 'file not found' or 'server down'.
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(FileURL);
System.Net.HttpWebResponse resp = (System.Net.HttpWebResponse)req.GetResponse();
System.IO.Stream f1 = resp.GetResponseStream();
System.IO.Stream f2 = new System.IO.FileStream(LocalDestination, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write);
byte[] b = new byte[1000];
int n;
while ((n = f1.Read(b, 0, b.Length)) > 0)
{
f2.Write(b, 0, n);
}
f2.Close();
f1.Close();
resp.Close();
}
}
}
------------------------------------------------
- kan du give mig et hint? :]