C#實現線上更新軟體

來源:互聯網
上載者:User

C#實現線上更新軟體

  winform程式相對web程式而言,功能更強大,編程更方便,但軟體更新卻相當麻煩,要到用戶端一台一台地升級,面對這個實際問題,在最近的一個小項目中,本人設計了一個通過軟體實現自動升級技術方案,彌補了這一缺陷,有較好的參考價值

  通過某些手段後台更新軟體。首先你要有一個放置新版本資訊的網站

  UpdateSoftwareForm.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

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

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using CCWin;

using System.Net;

using System.Collections;

using System.IO;

using System.Xml;

using System.Diagnostics;

using System.Threading;

 

namespace WriteBook

{

public partial class UpdateSoftwareForm : Skin_Metro

{

public UpdateSoftwareForm()

{

InitializeComponent();

}

 

#region 一些對象和變數

 

//使用WebClient下載

WebClient client = new WebClient();

ArrayList downlist = new ArrayList();

//目前的版本

string nowversion = null;

//最新版本

string latesversion = null;

 

#endregion

 

#region 擷取版本號碼

 

/// <summary>

/// 從伺服器上擷取最新的版本號碼

/// </summary>

public void DownloadCheckUpdateXml()

{

try

{

//第一個參數是檔案的地址,第二個參數是檔案儲存的路徑檔案名稱

client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WriteBook2.xml", "WriteBook2.xml");

}

catch

{

MessageBox.Show("沒有檢測到更新。", "提示");

this.Close();

}

}

 

/// <summary>

/// 擷取本地軟體的版本號碼

/// </summary>

private void NowVersion()

{

nowversion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString() + "\n";

LocalText.Text = nowversion;

}

 

/// <summary>

/// 讀取從伺服器擷取的最新版本號碼

/// </summary>

public void LatestVersion()

{

try

{

if (File.Exists("WriteBook2.xml.xml"))

{

XmlDocument doc = new XmlDocument();

//載入要讀取的XML

doc.Load("WriteBook2.xml.xml");

 

//獲得根節點

XmlElement WriteBook = doc.DocumentElement;

 

//獲得子節點 返回節點的集合

XmlNodeList Update = WriteBook.ChildNodes;

 

foreach (XmlNode item in Update)

{

latesversion = item.InnerText;

}

LatestText.Text = latesversion;

}

else

{

MessageBox.Show("沒有檢測到更新。", "提示");

this.Close();

}

}

catch

{

this.Close();

}

}

 

#endregion

 

#region 初始化程式

 

/// <summary>

/// 初始化程式

/// </summary>

private void InitializeandInstall()

{

UpdateProgressBar.Value = 20;

DownloadCheckUpdateXml();

UpdateProgressBar.Value = 40;

NowVersion();

UpdateProgressBar.Value = 60;

LatestVersion();

UpdateProgressBar.Value = 80;

DownloadInstall();

UpdateProgressBar.Value = 100;

}

 

#endregion

 

#region 安裝and刪除

 

/// <summary>

/// 下載安裝包

/// </summary>

public void DownloadInstall()

{

try

{

if (nowversion == latesversion)

{

MessageBox.Show("您已經是最新版本。", "提示");

}

else if (nowversion != latesversion && File.Exists("WriteBook2.xml"))

{

MessageBox.Show("發現新版本,即將下載更新補丁。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WBsetup.exe", "WBsetup.exe");

if (File.Exists("Setup.exe"))

{

InstallandDelete();

}

else

{

for (int i = 1; i < 3; i++)

{

client.DownloadFile("http://bbs.cloudtour.tk/SoftwareDownload/WriteBook/WBsetup.exe", "WBsetup.exe");

}

MessageBox.Show("下載失敗,請檢查您的網路連接是否正常。", "提示");

this.Close();

}

}

}

catch

{

MessageBox.Show("更新失敗,沒有發現新版本。", "提示");

this.Close();

}

}

 

/// <summary>

/// 安裝及刪除

/// </summary>

private void InstallandDelete()

{

try

{

DialogResult dr = MessageBox.Show("下載更新成功,是否安裝新更新?", "提示", MessageBoxButtons.YesNoCancel);

if (dr == System.Windows.Forms.DialogResult.Yes)

{

//啟動安裝程式

System.Diagnostics.Process.Start("WBsetup.exe");

Thread td = new Thread(JudgeInstall);

td.Start();

}

else { }

}

catch

{

MessageBox.Show("發生未知錯誤,更新失敗。", "提示");

this.Close();

}

}

 

/// <summary>

/// 判斷安裝進程是否存在

/// </summary>

public void JudgeInstall()

{

while (true)

{

Process[] processList = Process.GetProcesses();

foreach (Process process in processList)

{

if (process.ProcessName == "WBsetup.exe") { }

else

{

DialogResult dr = MessageBox.Show("更新成功,是否刪除安裝包?", "提示", MessageBoxButtons.YesNo);

if (dr == System.Windows.Forms.DialogResult.Yes)

{

File.Delete("WBsetup.exe");

File.Delete("WriteBook2.xml");

}

}

}

}

}

 

#endregion

 

/// <summary>

/// 點擊初始化程式

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void UpdateButton_Click(object sender, EventArgs e)

{

InitializeandInstall();

}

}

}

  以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.