Code:
public class Autopatcher
{
/// <summary>
/// The url.
/// </summary>
private string m_url;
/// <summary>
/// Get's the URL of the autopatcher.
/// </summary>
/// <returns>The correct url.</returns>
public string GetURL()
{
return (m_url.EndsWith("/") ? m_url : m_url + "/");
}
/// <summary>
/// Creating a new instance of an autopatcher.
/// </summary>
/// <param name="URL">The url where the patches are located. Ex. http://127.0.0.1/autopatcher/ </param>
public Autopatcher(string URL)
{
m_url = URL;
}
/// <summary>
/// The progress percentage.
/// </summary>
private int Progress = 0;
/// <summary>
/// Get's the progress percentage of the patching.
/// </summary>
/// <returns>The progress percentage.</returns>
public int GetProgress()
{
return Progress;
}
/// <summary>
/// The current patch.
/// </summary>
private int CurrentPatch;
/// <summary>
/// Get's the current patch.
/// </summary>
/// <returns>The current patch.</returns>
public int GetCurrentPatch()
{
return CurrentPatch;
}
/// <summary>
/// The patch result.
/// </summary>
private PatchResult Result;
/// <summary>
/// Get's the current patch result.
/// </summary>
/// <returns>The patch result.</returns>
public PatchResult GetLastResult()
{
return Result;
}
/// <summary>
/// Begins a new patch download.
/// </summary>
/// <param name="_CurrentPatch">The current patch to download.</param>
public void BeginPatch(int _CurrentPatch)
{
CurrentPatch = _CurrentPatch;
Result = Patch();
}
/// <summary>
/// This method is called from BeginPatch(int) and will return Result.
/// </summary>
/// <returns>Result</returns>
private PatchResult Patch()
{
Result = new PatchResult();
try
{
Result.Error = false;
WebClient client = new WebClient();
int targetpatch = int.Parse(client.DownloadString(GetURL() + "version.dat"));
if (targetpatch > CurrentPatch)
{
Result.UpToDate = false;
Uri uri = new Uri(m_url + CurrentPatch + "/");
client.DownloadFileAsync(uri, CurrentPatch + ".rar");
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(BeginProgress);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(BeginEndPatch);
}
else
Result.UpToDate = true;
return Result;
}
catch (Exception e)
{
Result.Error = true;
Result.exception = e;
return Result;
}
}
/// <summary>
/// The progress event.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BeginProgress(object sender, DownloadProgressChangedEventArgs e)
{
Progress += e.ProgressPercentage;
}
/// <summary>
/// The end event.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BeginEndPatch(object sender, AsyncCompletedEventArgs e)
{
CurrentPatch++;
BeginPatch(CurrentPatch);
}
}
Code:
public class PatchResult
{
public bool Error;
public bool UpToDate;
public Exception exception;
}
From an old autopatch library I made, figure out how to use or improve it yourself.