權(quán)限管理系統(tǒng)系列之序言
權(quán)限管理系統(tǒng)系列之WCF通信
之前寫了兩篇關(guān)于權(quán)限管理系統(tǒng)的博客了,由于這段時(shí)間有事比較忙就暫停了,今天繼續(xù)編寫權(quán)限管理系統(tǒng)之登陸和升級(jí)模塊,登陸和升級(jí)也是每個(gè)系統(tǒng)之必須的模塊,對(duì)于一個(gè)Winform程序,在登陸之前必須要先進(jìn)行程序的升級(jí),所以先介紹升級(jí)模塊。
表結(jié)構(gòu)如下:

插入數(shù)據(jù)表數(shù)據(jù)如下:

一般程序升級(jí)可能有好幾種,比如說有根本版本號(hào)比較升級(jí)、根據(jù)DLL生成時(shí)間比較等等,而我介紹的是根據(jù)版本號(hào)進(jìn)行升級(jí)。我們需要在服務(wù)端將DLL文件的 版本寫入到數(shù)據(jù)表中,這樣供每個(gè)客戶端去比較版本號(hào),就是每次打開客戶端之前進(jìn)行檢測(cè)版本號(hào),如果版本號(hào)相等則不升級(jí),如果不相等則進(jìn)行升級(jí)操作。
服務(wù)端實(shí)現(xiàn)邏輯如下:
1 /// <summary> 2 /// 更新客戶端程序集信息 3 /// </summary> 4 public static void UpdateAssembleInfo() 5 { 6 DbHelper dbhelper = new DbHelper(AppServer.dbName); 7 string sql = "select * from t_fw_assemble_list t"; 8 DataTable dt = dbhelper.Query(sql); 9 DirectoryInfo dirInfo = new DirectoryInfo(application.StartupPath);10 FileVersionInfo fvi = null;11 string updateSql = "update t_fw_assemble_list set s_versionno= '{0}',t_timestamp=getdate() where s_type='{1}' and s_filename='{2}'";12 string fName = string.Empty;13 GetAllFiles(dirInfo);14 bool isAddLog = true;15 StreamReader sr = null;16 for (int i = 0; i < dt.Rows.Count; i++)17 {18 fName = dt.Rows[i]["S_FILENAME"].ToString();19 if (fileNameDic.ContainsKey(fName))20 {21 if (dt.Rows[i]["L_UPDATE"].ToString() == "2")22 {23 sr = new StreamReader(Application.StartupPath + "http://" + fName);24 string fileContext = sr.ReadToEnd();25 sr.Close();26 sr.Dispose();27 sql = string.Format(updateSql, Common.Util.md5Encrypt.MD5EncryptDES(fileContext), dt.Rows[i]["S_TYPE"], fName);28 dbhelper.ExecuteSql(sql);29 isAddLog = false;30 }31 else32 {33 fvi = FileVersionInfo.GetVersionInfo(fileNameDic[fName]);34 if (!dt.Rows[i]["S_VERSIONNO"].ToString().Equals(fvi.FileVersion))35 {36 sql = string.Format(updateSql, fvi.FileVersion, dt.Rows[i]["S_TYPE"], fName);37 dbhelper.ExecuteSql(sql);38 isAddLog = false;39 }40 }41 if (!isAddLog)42 {43 isAddLog = AddLog();44 }45 }46 }47 }以上代碼主要是寫入版本到數(shù)據(jù)庫(kù)里,每次服務(wù)端啟動(dòng)首先執(zhí)行這段代碼。
服務(wù)端搞定我就來看看客戶端了,客戶端啟動(dòng)時(shí)調(diào)用以下方法實(shí)現(xiàn):
1 bool isDownLoad = false; 2 if (args != null) 3 { 4 if (args.Length > 0) 5 { 6 for (int i = 0; i < args.Length; i++) 7 { 8 if (args[i] == "Update") 9 {10 isDownLoad = true;11 break;12 }13 }14 }15 }16 if (isDownLoad)17 {18 //啟動(dòng)主界面19 Application.Run(new LoginForm());20 }21 else22 {23 //更新客戶端程序集24 if (UpdateAssembleData.UpdateAssembleInfo() > 0)25 {26 DownLoadForm dlf = new DownLoadForm();27 dlf.fileNameList = UpdateAssembleData.fileNameList;28 //啟動(dòng)下載程序界面29 Application.Run(dlf);30 }31 else32 {33 //啟動(dòng)主界面34 Application.Run(new LoginForm());35 }36 }
更新時(shí)會(huì)彈出升級(jí)窗體,上面會(huì)顯示升級(jí)的DLL文件,以及文件升級(jí)的進(jìn)度條,升級(jí)完成啟動(dòng)新的程序,升級(jí)過程的核心代碼:
1 /// <summary> 2 /// 下載文件 3 /// </summary> 4 PRivate void DownLoadFile() 5 { 6 if (fileNameList.Count > 0) 7 { 8 //CallService service = new CallService("GetFile"); 9 int countLen = fileNameList.Count; 10 this.pbarDownLoad.Position = this.pbarDownLoad.Properties.Minimum; 11 double step = this.pbarDownLoad.Properties.Maximum / countLen; 12 string fName = string.Empty; 13 string upPath = Application.StartupPath + "http://Update//"; 14 if (!Directory.Exists(upPath)) 15 { 16 Directory.CreateDirectory(upPath); 17 } 18 19 string sql = string.Empty; 20 FileStream fs; 21 bool isStartUpdate = false; 22 List<string> list = new List<string>(); 23 List<string> getList = new List<string>(); 24 //int fLen = 0; 25 long pageNum = 0; 26 for (int i = 0; i < countLen; i++) 27 { 28 bool isFirstPBLen = true; 29 isStartUpdate = false; 30 fName = fileNameList[i]; 31 IAsyncResult iart = this.lblDownLoad.BeginInvoke(new SetLabelText(AsyncLabel), new object[] { "正在下載文件 " + fName }); 32 this.lblDownLoad.EndInvoke(iart); 33 pageNum = 1; 34 list.Clear(); 35 list.Add(fName); 36 list.Add(pageNum.ToString ()); 37 38 //創(chuàng)建服務(wù)器下載的文件 39 string tmpPath = upPath + fName; 40 tmpPath = tmpPath.Substring(0, tmpPath.LastIndexOf('//')); 41 if (!Directory .Exists (tmpPath)) 42 { 43 Directory.CreateDirectory(tmpPath); 44 } 45 fs = new FileStream(upPath + fName, FileMode.Create, Fileaccess.Write); 46 47 while (true) 48 { 49 Result result = FileData.DoGetFile(list); 50 if (!result.success)//DoGetFile 51 { 52 Comm.WriteLogAndShowMessageBox.Error(result.msg, "Client.Win.DownLoadForm.DownLoadFile()出錯(cuò):" + result.msg); 53 StartUpdateApp(isStartUpdate); 54 break; 55 } 56 else 57 { 58 getList = JSON.Json2Object<List<string>>(result.data); //service.GetResult<List<string>>(); 59 byte[] buffer = Convert.FromBase64String(getList[2]); 60 61 if (isFirstPBLen) 62 { 63 //初始化當(dāng)前文件進(jìn)度條 64 iart = this.pbarCurrDownLoad.BeginInvoke(new UpdateProcessBar(AsyncIni), new object[] { Convert.ToInt32 (getList[0]), this.pbarCurrDownLoad }); 65 this.pbarCurrDownLoad.EndInvoke(iart); 66 isFirstPBLen = false; 67 Thread.Sleep(100); 68 } 69 70 //接收服務(wù)器返回的二制數(shù)據(jù) 71 fs.Write(buffer, 0, buffer.Length); 72 pageNum ++; 73 list[1] = pageNum.T
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注