Download text data from the specified URI
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Net;
using System.IO;
using System.ComponentModel;
namespace TrayWeather.Utils
{
/// <summary>
/// Download text data from the specified URI
/// </summary>
class DownloadText
{
private BackgroundWorker textDownloader;
public delegate void DownloadedEventHandler(string Result);
public event DownloadedEventHandler DataDownloaded;
public DownloadText()
{
textDownloader = new System.ComponentModel.BackgroundWorker();
}
/// <summary>
/// Download text data from the specified URI
/// </summary>
/// <param name="Uri">Text data uri</param>
/// <param name="Codepage">Text codepage</param>
public void GetTextAsync(Uri Uri, Encoding Codepage)
{
textDownloader.DoWork += new DoWorkEventHandler(xmlDownloader_DoWork);
textDownloader.RunWorkerCompleted += new RunWorkerCompletedEventHandler(xmlDownloader_RunWorkerCompleted);
GetTextArgs data = new GetTextArgs(Uri, Codepage);
textDownloader.RunWorkerAsync(data);
}
private void xmlDownloader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
//Under Development
//******************************************************
//if (Settings.Default.DisplayNetworkErrors)
// throw e.Error;
}
else
{
this.DataDownloaded((string)e.Result);
}
}
private void xmlDownloader_DoWork(object sender, DoWorkEventArgs e)
{
GetTextArgs data = ((GetTextArgs)e.Argument);
Encoding encode = System.Text.Encoding.UTF8;
if (data.dataCodepage != null)
encode = data.dataCodepage;
e.Result = GetText(data.dataUri, data.dataCodepage);
}
private struct GetTextArgs
{
public GetTextArgs(Uri XmlUri, Encoding Codepage)
{
dataUri = XmlUri;
dataCodepage = Codepage;
}
public Uri dataUri;
public Encoding dataCodepage;
}
}
}
Related examples in the same category