DMSSSOUpgrad.cs
7.75 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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using System.Xml;
using System.Diagnostics;
using System.Threading;
using System.Net;
namespace DMSSSOUpgrade
{
public partial class DMSSSOUpgrad : Form
{
// private string filePath = ConfigurationSettings.AppSettings["FilePath"].ToString();
public DMSSSOUpgrad()
{
InitializeComponent();
}
/// <summary>
/// 传递项目的资源文件路径
/// </summary>
/// <param name="_filePath"></param>
public DMSSSOUpgrad(string _filePath)
{
InitializeComponent();
//filePath = _filePath;
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
this.Dispose();
Application.Exit();
}
/// <summary>
/// 最小化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnMini_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
/// <summary>
/// 取消按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCancel_Click(object sender, EventArgs e)
{
Application.Exit();
}
/// <summary>
/// 下一步按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNext_Click(object sender, EventArgs e)
{
try
{
DMSSSOService.SSOServices ss = new DMSSSOUpgrade.DMSSSOService.SSOServices();
object[] oSysList = ss.getUpdateInfor();
if (((System.Xml.XmlNode[])(oSysList[0]))[0].Value != ConfigurationSettings.AppSettings["VersionNo"].ToString())
{
lbState.Text = "正在下载更新文件,请稍后...";
string[] fileList = ((System.Xml.XmlNode[])(oSysList[1]))[0].Value.Split('|');
//更新文件的文件地址 D:\Project\TNT\P-DMS SSO\Code\DMSSSO\DMSSSOAgent\bin\Debug
string updateSysFilePath = ConfigurationSettings.AppSettings["ServerPath"].ToString();
//滚动条
pbDownFile.Maximum = fileList.Length+2;
pbDownFile.Minimum = 0;
System.Net.WebClient webClient = new System.Net.WebClient();
for (int i = 0; i < fileList.Length; i++)
{
if (fileList[i].ToString() != "")
{
string filePathName = Application.StartupPath + fileList[i].ToString().Replace(" ", "");
string updateSysFilePathName=@""+updateSysFilePath+ fileList[i].ToString().Replace(" ","");
try
{
System.Net.WebRequest s = System.Net.WebRequest.Create(updateSysFilePathName);
System.Net.WebResponse a = s.GetResponse();
s.Abort();
}
catch
{
MessageBox.Show("更新失败," + fileList[i].ToString() + "文件不存在!");
btnNext.Enabled = false;
return;
}
if (File.Exists(filePathName))
{
//删除原始的
File.Delete(filePathName);
}
//放入新的文件
//File.Copy(updateSysFilePathName,filePathName);
try
{
webClient.DownloadFile(updateSysFilePathName, filePathName);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
pbDownFile.Value++;
}
}
else
{
MessageBox.Show("你目前的版本已是最新版本,没有可更新的内容!");
btnNext.Enabled = false;
return;
}
//修改版本号
//获得DMSSSOAgent 的 config 文件
string strFileNameAgent = Application.StartupPath + @"\DMSSSOAgent.exe.config";
if(File.Exists(strFileNameAgent))
{
SaveConfig(((System.Xml.XmlNode[])(oSysList[0]))[0].Value, "VersionNo", strFileNameAgent);
}
pbDownFile.Value++;
//获得DMSSSOUpgrade 的 config 文件
string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
SaveConfig(((System.Xml.XmlNode[])(oSysList[0]))[0].Value, "VersionNo",strFileName);
pbDownFile.Value++;
lbState.Text = "程序已更新完毕...";
//更新按钮状态
btnNext.Enabled = false;
btnFinish.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show("更新文件下载失败!" + ex.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 完成按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFinish_Click(object sender, EventArgs e)
{
Application.Exit();
Process.Start("DMSSSOAgent.exe", Application.StartupPath + @"\DMSSSOAgent.exe");
}
#region Load
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DMSSSOUpgrad_Load(object sender, EventArgs e)
{
btnFinish.Enabled = false;
}
#endregion
#region Update Config
/// <summary>
/// 方法保存修改的设置
/// </summary>
/// <param name="ConnenctionString"></param>
/// <param name="strKey"></param>
private void SaveConfig(string ConnenctionString, string strKey,string strFileName)
{
XmlDocument doc = new XmlDocument();
doc.Load(strFileName);
//找出名称为“add”的所有元素
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (att.Value == strKey)
{
//对目标元素中的第二个属性赋值
att = nodes[i].Attributes["value"];
att.Value = ConnenctionString;
break;
}
}
//保存上面的修改
doc.Save(strFileName);
}
#endregion
}
}