-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathDownloader.cs
92 lines (81 loc) · 3.05 KB
/
Downloader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
namespace LiveSplit.MemoryGraph
{
class MonkeyDownloadingXML
{
public bool DownloadNew()
{
string uriToSource = "https://raw.githubusercontent.com/kugelrund/LiveSplit.MemoryGraph/master/XML/";
string xmlFileName = Settings.listsFile;
bool result = false;
string downloadedFileLocation = "";
if (CheckIfXMLExists(uriToSource+ xmlFileName))
{
result = downloadFiles(uriToSource, xmlFileName, out downloadedFileLocation);
}
else
{
MessageBox.Show("No XML server found on a server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
if (result)
{
string currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
File.Copy(downloadedFileLocation, Path.Combine(currentPath,xmlFileName), true);
Debug.WriteLine("COPY: " + downloadedFileLocation + " --> " + Path.Combine(currentPath, xmlFileName));
MessageBox.Show("Successfully dowloaded new XML from a server", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
else
{
MessageBox.Show("Failed to download the file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
private bool downloadFiles(string sourceLocation, string file, out string tempLocation)
{
WebClient wbClient = new WebClient();
wbClient.DownloadFileCompleted += WbClient_DownloadFileCompleted;
tempLocation = Path.GetTempFileName();
Uri tempUri;
Uri.TryCreate(sourceLocation + file, UriKind.Absolute, out tempUri);
try { wbClient.DownloadFile(tempUri, tempLocation); }
catch { return false; }
FileInfo tempFileInfo = new FileInfo(tempLocation);
if (tempFileInfo.Length == 0)
{
return false;
}
return true;
}
private void WbClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
Trace.WriteLine("Error downloading file!");
}
else
{
Trace.WriteLine("Downloading file completed.");
}
}
private bool CheckIfXMLExists(string uri)
{
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
resp.Close();
return resp.StatusCode == HttpStatusCode.OK;
}
catch { return false; }
}
}
}