using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.ComponentModel;
using System.Xml;
using System.Diagnostics;
namespace Animaonline.Globals
{
[DebuggerNonUserCode]
public class IO
{
#region Network
public class Network
{
#region Constant Variables
const string BYTEORDERMARK = "?";
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
public Network()
{
NetworkClient.DownloadFileCompleted += new AsyncCompletedEventHandler(NetworkClient_DownloadFileCompleted);
NetworkClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(NetworkClient_DownloadProgressChanged);
}
#endregion
#region Event Triggers
///
/// Handles the DownloadFileCompleted event of the NetworkClient control.
///
/// The source of the event.
/// The instance containing the event data.
static void NetworkClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{ if (DownloadFileCompleted != null)DownloadFileCompleted(sender, e); }
///
/// Handles the DownloadProgressChanged event of the NetworkClient control.
///
/// The source of the event.
/// The instance containing the event data.
static void NetworkClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{ if (DownloadProgressChanged != null)DownloadProgressChanged(sender, e); }
#endregion
#region Members
///
/// Provides common methods for sending data to and receiving data from a resource identified by a URI.
///
private static WebClient NetworkClient;
#endregion
#region Events
///
/// Occurs when an asynchronous file download operation completes.
///
public static event AsyncCompletedEventHandler DownloadFileCompleted;
///
/// Occurs when an asynchronous download operation successfully transfers some or all of the data.
///
public static event DownloadProgressChangedEventHandler DownloadProgressChanged;
#endregion
#region Static Methods
///
/// Downloads the file from FTP.
///
/// The address.
/// The credentials.
/// Name of the file.
public static void DownloadFileFromFtp(Uri address, NetworkCredential credentials, string fileName)
{
FtpWebRequest ftpReq;
try
{
using (FileStream outputStream = new FileStream(fileName, FileMode.Create))
{
ftpReq = (FtpWebRequest)FtpWebRequest.Create(address);
ftpReq.Method = WebRequestMethods.Ftp.DownloadFile;
ftpReq.UseBinary = true;
ftpReq.Credentials = credentials;
FtpWebResponse response = (FtpWebResponse)ftpReq.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
}
catch (Exception DownloadFileException)
{
throw DownloadFileException;
}
}
///
/// Downloads the resource with the specified URI to a local file.
///
/// The URI from which to download data.
/// The name of the local file that is to receive the data.
///
///
public static void DownloadFile(Uri address, string fileName) { DownloadFile(address.ToString(), fileName, null); }
///
/// Downloads the resource with the specified URI to a local file.
///
/// The URI from which to download data.
/// The name of the local file that is to receive the data.
///
///
public static void DownloadFile(string address, string fileName) { DownloadFile(address, fileName, null); }
///
/// Downloads the resource with the specified URI to a local file.
///
/// The URI from which to download data.
/// The name of the local file that is to receive the data.
///
///
public static void DownloadFile(string address, string fileName, string cookie)
{
using (NetworkClient = new WebClient())
{
if (cookie != null)
{
NetworkClient.Headers[HttpRequestHeader.Cookie] = cookie;
NetworkClient.DownloadFile(address, fileName);
}
else
{
NetworkClient.DownloadFile(address, fileName);
}
}
}
///
/// Downloads XML Data with the specified URI
///
/// The URI from which to download XML data.
///
///
///
public static XmlDocument DownloadXmlDocument(string address) { return DownloadXmlDocument(address, null); }
///
/// Downloads XML Data with the specified URI
///
/// The URI from which to download XML data.
///
///
///
public static XmlDocument DownloadXmlDocument(string address, string cookie)
{
XmlDocument xDoc = new XmlDocument();
using (NetworkClient = new WebClient())
{
if (cookie != null)
{
NetworkClient.Headers[HttpRequestHeader.Cookie] = cookie;
string InnerXml = NetworkClient.DownloadString(address);
#region WebClient HACK - Fixes Byte Order Mark (BOM)
if (InnerXml.Contains(BYTEORDERMARK))
{
InnerXml = InnerXml.Replace(BYTEORDERMARK, string.Empty);
}
#endregion
xDoc.LoadXml(InnerXml);
return xDoc;
}
else
{
xDoc.LoadXml(NetworkClient.DownloadString(address));
return xDoc;
}
}
}
///
/// Downloads, to a local file, the resource with the specified URI. This method does not block the calling thread.
///
/// The URI of the resource to download.
/// The name of the file to be placed on the local computer.
///
///
public static void DownloadFileAsync(string address, string fileName) { DownloadFileAsync(new Uri(address), fileName, null); }
///
/// Downloads, to a local file, the resource with the specified URI. This method does not block the calling thread.
///
/// The URI of the resource to download.
/// The name of the file to be placed on the local computer.
///
///
public static void DownloadFileAsync(Uri address, string fileName) { DownloadFileAsync(address, fileName, null); }
///
/// Downloads, to a local file, the resource with the specified URI. This method does not block the calling thread.
///
/// The URI of the resource to download.
/// The name of the file to be placed on the local computer.
///
///
public static void DownloadFileAsync(Uri address, string fileName, string cookie)
{
using (NetworkClient = new WebClient())
{
#region Event Subscription
NetworkClient.DownloadFileCompleted += NetworkClient_DownloadFileCompleted;
NetworkClient.DownloadProgressChanged += NetworkClient_DownloadProgressChanged;
#endregion
if (cookie != null)
{
NetworkClient.Headers[HttpRequestHeader.Cookie] = cookie;
NetworkClient.DownloadFileAsync(address, fileName);
}
else
{
NetworkClient.DownloadFileAsync(address, fileName);
}
}
}
#endregion
}
#endregion
}
}