示例#1
0
 //复制文件;
 public void CopyFile(string sourcePath, string objPath)
 {
     if (!Directory.Exists(objPath))
     {
         Directory.CreateDirectory(objPath);
     }
     string[] files = Directory.GetFiles(sourcePath);
     for (int i = 0; i < files.Length; i++)
     {
         string[] childfile = files[i].Split('\\');
         FileCopyCounter++;
         WindowFormDelegate.SetMainThreadHint(this.lbFileListHint, string.Format("正在复制第{0}个文件{1}", FileCopyCounter.ToString(), childfile[childfile.Length - 1]));
         File.Copy(files[i], objPath + @"\" + childfile[childfile.Length - 1], true);
     }
     string[] dirs = Directory.GetDirectories(sourcePath);
     for (int i = 0; i < dirs.Length; i++)
     {
         string[] childdir = dirs[i].Split('\\');
         CopyFile(dirs[i], objPath + @"\" + childdir[childdir.Length - 1]);
     }
 }
示例#2
0
        private void DownUpdateFile()
        {
            mainAppExe = updaterXmlFiles.GetNodeValue("//EntryPoint");
            Process [] allProcess = Process.GetProcesses();
            foreach (Process p in allProcess)
            {
                if (p.ProcessName.ToLower() + ".exe" == mainAppExe.ToLower())
                {
                    for (int i = 0; i < p.Threads.Count; i++)
                    {
                        p.Threads[i].Dispose();
                    }
                    p.Kill();
                    isRun = true;
                }
            }
            bool        isFinish = false;
            WebClient   wcClient = AppUpdater.CreateClient();
            WebResponse webRes   = null;
            WebRequest  webReq   = null;

            for (int i = 0; i < this.lvUpdateList.Items.Count; i++)
            {
                string UpdateFile = lvUpdateList.Items[i].Text.Trim();
                //string UpdateFile = WindowFormDelegate.GetListViewText(lvUpdateList, i);
                string updateFileUrl = updateUrl + UpdateFile;
                long   fileLength    = 0;
                WindowFormDelegate.SetMainThreadHint(lbFileListHint, string.Format("共{0}个文件待更新,准备更新第{1}个文件中", lvUpdateList.Items.Count.ToString(), (i + 1).ToString()));
                try
                {
                    TempLog.Debug(string.Format("下载更新文件{0},创建WebRequest", updateFileUrl));
                    webReq = AppUpdater.Create(updateFileUrl);
                    TempLog.Debug("完成创建WebRequest");
                    TempLog.Debug("下载更新文件,准备获取WebResponse");
                    webRes = webReq.GetResponse();
                    TempLog.Debug("下载更新文件,完成获取WebResponse");
                    fileLength = webRes.ContentLength;
                    WindowFormDelegate.SetMainThreadHint(this.lbState, "正在下载更新文件,请稍后...");
                    //lbState.Text = "正在下载更新文件,请稍后...";
                    pbDownFile.Value   = 0;
                    pbDownFile.Maximum = (int)fileLength;

                    TempLog.Debug("下载更新文件,获取GetResponseStream");
                    Stream srm = webRes.GetResponseStream();
                    TempLog.Debug("下载更新文件,完成获取GetResponseStream");
                    StreamReader srmReader  = new StreamReader(srm);
                    byte[]       bufferbyte = new byte[fileLength];
                    int          allByte    = (int)bufferbyte.Length;
                    int          startByte  = 0;
                    while (fileLength > 0)
                    {
                        Application.DoEvents();
                        int downByte = srm.Read(bufferbyte, startByte, allByte);
                        if (downByte == 0)
                        {
                            break;
                        }
                        ;
                        startByte        += downByte;
                        allByte          -= downByte;
                        pbDownFile.Value += downByte;

                        float part    = (float)startByte / 1024;
                        float total   = (float)bufferbyte.Length / 1024;
                        int   percent = Convert.ToInt32((part / total) * 100);

                        this.lvUpdateList.Items[i].SubItems[2].Text = percent.ToString() + "%";
                    }

                    string tempPath = tempUpdatePath + UpdateFile;
                    CreateDirtory(tempPath);
                    FileStream fs = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Write);
                    fs.Write(bufferbyte, 0, bufferbyte.Length);
                    srm.Close();
                    srmReader.Close();
                    fs.Close();
                }
                catch (WebException ex)
                {
                    //MessageBox.Show("更新文件下载失败!" + ex.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //  isFinish = false;
                    // continue;
                    Console.WriteLine("下载异常:" + ex.ToString());
                }
                finally
                {
                    if (webRes != null)
                    {
                        webRes.Close();
                        webReq.Abort();
                    }
                }
                if (i == this.lvUpdateList.Items.Count - 1)
                {
                    isFinish = true;
                    //this.btnFinish_Click(null, null);
                }
            }
            InvalidateControl();
            if (isFinish)
            {
                this.CopyFileThread();
            }
            //btnFinish_Click(null, null);
            else
            {
                this.StartMainApplication();
            }
        }