private void 显示已忽略ToolStripMenuItem_Click(object sender, EventArgs e) { ControlUtil.RefueshToControlComparisons(listView1, "已忽略"); }
/// <summary> /// MD5比对 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { listView1.Items.Clear(); ControlUtil.ClearComparisons(); string remoteServer = cb_remoteServer.Text; if (remoteServer == null || remoteServer == "" || cb_remoteServer.SelectedIndex == 0) { MessageBox.Show("请先选择远程服务器!"); cb_remoteServer.Focus(); return; } if (listBox1.Items.Count <= 0) { MessageBox.Show("请先选择本地项目文件,可直接拖放!"); return; } DialogResult result = MessageBox.Show("此操作耗时较长,可能会因为长时间阻塞导致页面无响应. 计算过程中请勿关闭窗口! 点击\"取消\"可撤回此操作. ", "MD5计算", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (result == DialogResult.OK) { //计算过程中先禁用此按钮,防止重复产生点击事件 button2.Text = "MD5(计算中...)"; button2.Enabled = false; List <Dictionary <string, string> > clientMD5s = new List <Dictionary <string, string> >(); for (int i = 0; i < listBox1.Items.Count; i++) { listBox1.SelectedIndex = i; string listTag = listBox1.Tag.ToString(); string path = listBox1.Items[i].ToString(); FileInfo fileinfo = new FileInfo(path); long size = fileinfo.Length; string md5Value = MD5Util.GetMD5HashFromFile(path); Dictionary <string, string> map = new Dictionary <string, string>(); path = path.Replace(listTag, "").Trim(); map.Add("md5", md5Value); map.Add("url", path); map.Add("size", Convert.ToString(size)); clientMD5s.Add(map); } //请求服务器计算服务端MD5值 string serverResponse = SocketUtil.SocketMD5(ClientCache.GetCurrentServerIP(), Convert.ToInt32(ClientCache.GetInterflowPort()), JSONUtil.RequestServerMD5InfoJsonStr(remoteServer)); List <Dictionary <string, string> > serverMD5s = JSONUtil.ParseServerMD5Info(serverResponse); List <string> usedList = new List <string>(); int count = 1; //开始比对和生成记录到ListView控件中,以本地项目为参照物. 因为一般情况下本地项目的文件会多于服务器端 foreach (Dictionary <string, string> client in clientMD5s)//Loop client { string clientUrl = client["url"]; string clientMd5 = client["md5"]; string clientSize = client["size"]; foreach (Dictionary <string, string> server in serverMD5s)//Loop server { string serverUrl = server["url"]; string serverMd5 = server["md5"]; string serverSize = server["size"]; //这一个替换在操作线上代码时格外重要! 线上Linux是/而windows是\\ serverUrl = serverUrl.Replace("/", "\\");//Linux. Windows下注释此代码 if (clientUrl.Equals(serverUrl)) { Dictionary <string, string> map = new Dictionary <string, string>(); map.Add("num", Convert.ToString(count++)); map.Add("urlremote", serverUrl); map.Add("urllocal", clientUrl); map.Add("sizeremote", serverSize); map.Add("sizelocal", clientSize); //存在相同的url,即文件 . 比对MD5值是否一致 if (clientMd5.Equals(serverMd5)) { //MD5也一致,视为没有任何更改 map.Add("result", "一致"); ControlUtil.AddListViewLineOfComparison(map, Color.White, "一致"); usedList.Add(clientUrl);//将客户端本数据标记为已匹配过 } else { //客户端和服务端同一个文件MD5不一致,视为客户端有更改. 需要后续上传 map.Add("result", "不一致"); ControlUtil.AddListViewLineOfComparison(map, Color.Red, "不一致"); usedList.Add(clientUrl);//将客户端本数据标记为已匹配过 } } } } //检查客户端剩下的没有检查过的数据,一般视为本地新增的数据 foreach (Dictionary <string, string> client in clientMD5s) { string clientUrl = client["url"]; string clientMd5 = client["md5"]; string clientSize = client["size"]; if (usedList.Contains(clientUrl)) { continue; } Dictionary <string, string> map = new Dictionary <string, string>(); map.Add("num", Convert.ToString(count++)); map.Add("urlremote", ""); map.Add("urllocal", clientUrl); map.Add("sizeremote", "0"); map.Add("sizelocal", clientSize); map.Add("result", "新增"); ControlUtil.AddListViewLineOfComparison(map, Color.Green, "新增"); } ControlUtil.RefueshToControlComparisons(listView1, "all"); button2.Text = "MD5"; button2.Enabled = true; } }