1 public partial class update : Form 2 { 3 private WebClient client; 4 int downfilenum = 0; //已下载文件数 5 int downlistnum = 0;//总下载文件数 6 Listlist; 7 private string URl; 8 private string fileName; 9 private const string applicationFile = "Setup"; 10 11 public update() 12 { 13 InitializeComponent(); 14 } 15 //检测网络状态 16 [DllImport("wininet.dll")] 17 private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue); 18 private void update_Load(object sender, EventArgs e) 19 { 20 if (isConnected()) 21 { 22 client = new WebClient(); 23 client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); 24 client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); 25 client.Proxy = WebRequest.DefaultWebProxy; 26 client.Proxy.Credentials = new NetworkCredential(); 27 updateFile(); 28 } 29 else 30 { 31 lblMsg.Text = "网络连接异常,请联系管理员处理"; 32 } 33 } 34 /// 35 /// 检测网络状态 36 /// 37 private bool isConnected() 38 { 39 int I = 0; 40 bool state = InternetGetConnectedState(out I, 0); 41 return state; 42 } 43 ///44 /// 获取xml文件,判断是否升级 45 /// 46 /// 47 ///48 private string getSoftUpdate() 49 { 50 if (!File.Exists(Application.StartupPath + @"\\update.xml")) 51 { 52 return "nofile"; 53 } 54 DateTime newDateTime; 55 try 56 { 57 SrmUpdate.updateSoapClient s = new SrmUpdate.updateSoapClient(); 58 newDateTime = Convert.ToDateTime(s.GetSoftUpDateTime());//获取服务器最新更新日期 59 } 60 catch (Exception) 61 { 62 63 throw; 64 } 65 DateTime oldDateTime; 66 XmlDocument doc = new XmlDocument(); 67 doc.Load(Application.StartupPath + @"\\update.xml"); 68 oldDateTime = Convert.ToDateTime(doc.SelectSingleNode("/AutoUpdate/SoftUpdateTime").Attributes["Date"].Value); 69 return newDateTime >= oldDateTime ? "true" : "false"; 70 } 71 /// 72 /// 开始下载文件 73 /// 74 private void updateFile() 75 { 76 //判断系统有无更新,如果需要升级系统,开始下载 77 string isUpdate = getSoftUpdate(); 78 if (isUpdate == "true") 79 { 80 lblMsg.Text = "检测到版本有更新,准备升级"; 81 progressBar1.Visible = true; 82 startDownload(); 83 } 84 else if (isUpdate == "false") 85 { 86 lblMsg.Text = "无需更新,直接启动系统"; 87 runSystem(); 88 } 89 else if (isUpdate == "nofile") 90 { 91 lblMsg.Text = "缺少系统文件,请您联系管理员处理"; 92 } 93 } 94 ///95 /// 下载完成调用 96 /// 97 /// 98 /// 99 void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) 100 { 101 downfilenum++; 102 if (client != null) { client.CancelAsync(); } 103 if (downfilenum < downlistnum) downLoadFile(list[downfilenum].ToString()); //下载剩余的 104 if (downfilenum == downlistnum) { init(); } //初始化 105 } 106 //下载某个文件 107 private void downLoadFile(string fileName) 108 { 109 downLoad(URl + fileName, Application.StartupPath + "\\temp\\" + fileName, true); //异步下载远程文件 110 } 111 ///112 /// 下载进度条 113 /// 114 /// 115 /// 116 void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 117 { 118 int a = e.ProgressPercentage; 119 progressBar1.Value = a; 120 lblMsg.Text = a + "% Downloading... "; 121 } 122 123 ///124 /// HTTP下载远程文件并保存本地的函数 125 /// 126 private void downLoad(string downAddress, string savePath, Boolean Async) 127 { 128 DirectoryInfo di = Directory.GetParent(savePath); 129 if (!di.Exists) di.Create(); 130 131 WebClient client = new WebClient(); //再次new 避免WebClient不能I/O并发 132 if (Async) 133 { //异步下载 134 client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); 135 client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); 136 client.DownloadFileAsync(new Uri(downAddress), savePath); 137 } 138 else client.DownloadFile(new Uri(downAddress), savePath); 139 } 140 ///141 /// 开始下载 142 /// 143 private void startDownload() 144 { 145 SrmUpdate.updateSoapClient ws = new SrmUpdate.updateSoapClient(); 146 URl = ws.GetSoftUpdateUrl(); 147 list = ws.GetSoftUpdateFileList(); 148 downlistnum = list.Count; 149 fileName = list[0].Substring(list[0].LastIndexOf("/") + 1, list[0].Length - list[0].LastIndexOf("/") - 1); 150 downLoad(URl + list[0], Application.StartupPath + "\\temp\\" + fileName, true); 151 } 152 private void init() 153 { 154 string curdir = Application.StartupPath; 155 DirectoryInfo theFolder = new DirectoryInfo(curdir + @"\temp"); 156 if (theFolder.Exists) 157 { 158 copyDirectory(curdir + @"\temp", curdir); 159 } 160 161 if (Directory.Exists(curdir + @"\temp\")) Directory.Delete(curdir + @"\temp\", true); //删除临时目录 162 runSystem(); 163 } 164 ///165 /// 复制文件夹中的所有文件到指定文件夹 166 /// 167 /// 源文件夹路径 168 /// 保存路径 169 private void copyDirectory(string DirectoryPath, string DirAddress)//复制文件夹, 170 { 171 if(!Directory.Exists(DirAddress)) Directory.CreateDirectory(DirAddress); 172 DirectoryInfo DirectoryArray = new DirectoryInfo(DirectoryPath); 173 FileInfo[] Files = DirectoryArray.GetFiles();//获取该文件夹下的文件列表 174 DirectoryInfo[] Directorys = DirectoryArray.GetDirectories();//获取该文件夹下的文件夹列表 175 foreach (FileInfo theFile in Files)//逐个复制文件 176 { 177 //如果临时文件夹下存在与应用程序所在目录下的文件同名的文件,则删除应用程序目录下的文件 178 if (File.Exists(DirAddress + "\\" + Path.GetFileName(theFile.FullName))) 179 File.Delete(DirAddress + "\\" + Path.GetFileName(theFile.FullName)); 180 //将临时文件夹的文件移到应用程序所在的目录下 181 File.Copy(theFile.FullName, DirAddress + "\\" + Path.GetFileName(theFile.FullName)); 182 } 183 foreach (DirectoryInfo Dir in Directorys)//逐个获取文件夹名称,并递归调用方法本身 184 { 185 copyDirectory(DirectoryPath + "\\" + Dir.Name, DirAddress + "\\" + Dir.Name); 186 } 187 } 188 ///189 /// 启动主程序 190 /// 191 private void runSystem() 192 { 193 //启动安装程序 194 this.lblMsg.Text = "正在启动主程序...."; 195 System.Diagnostics.Process.Start(Application.StartupPath + @"\SRMClientAppLoader.exe"); 196 this.Close(); 197 } 198 }