自己写的进度条
namespace Demo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string AllFile = string.Empty; ServiceReference1.UploadFileSoapClient soap = new ServiceReference1.UploadFileSoapClient(); private void button1_Click(object sender, EventArgs e) { AllFile = ""; string FtpFile = soap.GetAllFileName();//获取服务器返回的JSON,获取所有的文件名称,及文件修改时间 int AllFileLength = 0; var json = Newtonsoft.Json.Linq.JArray.Parse(FtpFile);//处理JSON var listFileName = json.Select(j => j["FileName"]).ToList();//获取服务器文件名称列表 var FileLength = json.Select(j => j["FileLength"]).ToList();//获取服务器文件字节 var FileTime = json.Select(j => j["FileTime"]).ToList();//获取服务器文件修改时间 for (int i = 0; i < FileLength.Count; i++) { if (!System.IO.File.Exists(Path.GetFullPath(".\\") + "UponLoad\\" + listFileName[i])) { AllFile += listFileName[i] + ","; //记录不存在的文件名称 AllFileLength += Convert.ToInt32(FileLength[i]);//记录不存在的文件大小 } else { FileInfo fileInfo = new FileInfo(Path.GetFullPath(".\\") + "UponLoad\\" + listFileName[i]); if (fileInfo.LastWriteTime.ToString("yyyyMMddHHmmss") != FileTime[i].ToString()) { AllFile += listFileName[i] + ",";//记录不存在的文件名称 AllFileLength += Convert.ToInt32(FileLength[i]);//记录不存在的文件大小 } } } //若不存在记录数,则不进行更新 if (AllFile.Length > 0) { AllFile = AllFile.Substring(0, AllFile.Length - 1); MessageBox.Show("需要更新的文件一共有" + AllFile.Split(',').Count() + "个,总共大小为:" + (AllFileLength / 1024) + "KB"); progressBar1.Maximum = AllFile.Split(',').Count(); Thread t = new Thread(new ThreadStart(GetBaseStringToFile)); t.Start(); } else { MessageBox.Show("不需要更新"); } } private delegate void SetPoint(int pot); #region 更新进度条 /// <summary> /// 更新进度条 /// </summary> /// <param name="ipos"></param> private void SetTextMessage(int ipos) { if (this.InvokeRequired) { SetPoint setpos = new SetPoint(SetTextMessage); this.Invoke(setpos, new object[] { ipos }); } else { this.label1.Text = ipos.ToString() + "/"+AllFile.Split(',').Count(); this.progressBar1.Value = Convert.ToInt32(ipos); } } #endregion int j = 0;//记录更新成功的文件数目 public void GetBaseStringToFile() { string[] fileToName=AllFile.Split(','); for (int i = 0; i < fileToName.Count(); i++) { if (Untity.StringToFile(soap.FileToStream(fileToName[i]), Path.GetFullPath(".\\") + "UponLoad\\" + fileToName[i])) //下载更新文件,返回true则增加进度条 { Thread.Sleep(500); j++; SetTextMessage(j); } } } } }
还没搞出怎么提示子线程加载完成。搞出来了然后就可以提示更新成功或失败。。。
#region 把经过base64编码的字符串保存为文件 /// <summary> /// 把经过base64编码的字符串保存为文件 /// </summary> /// <param name="base64String">经base64加码后的字符串 </param> /// <param name="fileName">保存文件的路径和文件名 </param> /// <returns>保存文件是否成功 </returns> public static bool StringToFile(string base64String, string fileName) { try { FileStream fs = new FileStream(fileName, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); if (!string.IsNullOrEmpty(base64String) && File.Exists(fileName)) { bw.Write(Convert.FromBase64String(base64String)); } bw.Close(); fs.Close(); } catch { return false; } return true; } #endregion