-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSharpUpdateXml.cs
More file actions
164 lines (145 loc) · 5.5 KB
/
SharpUpdateXml.cs
File metadata and controls
164 lines (145 loc) · 5.5 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Net;
using System.Xml;
using System.Collections.Generic;
using System.IO;
namespace SharpUpdate
{
/// <summary>
/// Contains update information
/// </summary>
internal class SharpUpdateXml
{
private Version version;
private List<SharpUpdateFileInfo> updateFiles;
private string launchArgs;
private string description;
/// <summary>
/// The update version #
/// </summary>
internal Version Version
{
get { return this.version; }
}
/// <summary>
/// The description of the new version
/// </summary>
internal string Description
{
get { return description; }
//set { description = value; }
}
/// <summary>
/// All files that are available
/// </summary>
internal List<SharpUpdateFileInfo> UpdateFiles
{
get { return this.updateFiles; }
}
/// <summary>
/// The arguments to pass to the updated application on startup
/// </summary>
internal string LaunchArgs
{
get { return this.launchArgs; }
}
/// <summary>
/// Creates a new SharpUpdateXml object
/// </summary>
/// <param name="version">Application's current version</param>
/// <param name="updateFiles">Application's update files</param>
/// <param name="description">Update description</param>
/// <param name="launchArgs">Application's launch arguments</param>
internal SharpUpdateXml(Version version, List<SharpUpdateFileInfo> updateFiles, string description, string launchArgs)
{
this.version = version;
this.updateFiles = updateFiles;
this.description = description;
this.launchArgs = launchArgs;
}
/// <summary>
/// Checks if update's version is newer than the old version
/// </summary>
/// <param name="version">Application's current version</param>
/// <returns>True, if the update's version # is newer</returns>
internal bool IsNewerThan(Version version)
{
return this.version > version;
}
/// <summary>
/// Chacks if all files, specified in update.xml are in given folder
/// </summary>
/// <param name="path">Path to the folder, where the files should be</param>
/// <returns>True, if all files are there</returns>
internal bool HasAllFiles(string path)
{
string realPath = path;
if(!path.EndsWith("\\"))
realPath += "\\";
foreach (SharpUpdateFileInfo fi in this.updateFiles)
{
if (!File.Exists(realPath + fi.FileName))
return false;
}
return true;
}
/// <summary>
/// Checks the Uri to make sure file exist
/// </summary>
/// <param name="location">The Uri of the update.xml</param>
/// <returns>True, if the file exists</returns>
internal static bool ExistsOnServer(Uri location)
{
try
{
// Request the update.xml
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(location.AbsoluteUri);
// Read for response
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
resp.Close();
return resp.StatusCode == HttpStatusCode.OK;
}
catch { return false; }
}
/// <summary>
/// Parses the update.xml into SharpUpdateXml object
/// </summary>
/// <param name="location">Uri of update.xml on server</param>
/// <param name="appID">The application's ID</param>
/// <returns>The SharpUpdateXml object with the data, or null of any errors</returns>
internal static SharpUpdateXml Parse(Uri location, string appID)
{
Version version = null;
string launchArgs = "", description="";
List<SharpUpdateFileInfo> files = new List<SharpUpdateFileInfo>();
try
{
// Load the document
XmlDocument doc = new XmlDocument();
doc.Load(location.AbsoluteUri);
// Gets the appId's node with the update info
// This allows you to store all program's update nodes in one file
XmlNode updateNode = doc.DocumentElement.SelectSingleNode("//update[@appId='" + appID + "']");
// If the node doesn't exist, there is no update
if (updateNode == null)
return null;
// Parse data
version = Version.Parse(updateNode["version"].InnerText);
//Read each file
XmlNodeList fileNodes = updateNode.ChildNodes;
foreach (XmlNode xn in fileNodes)
{
if (xn.Name == "file")
{
files.Add(new SharpUpdateFileInfo(xn["url"].InnerText, xn["filename"].InnerText, xn["md5"].InnerText));
}
}
launchArgs = updateNode["launchArgs"].InnerText;
description = updateNode["description"].InnerText;
return new SharpUpdateXml(version, files, description, launchArgs);
}
catch
{ return null; }
}
}
}