Ejemplo n.º 1
0
        private void btnDownload_Click(object sender, EventArgs e)
        {
            btnDownload.Enabled = false;
            btnPause.Enabled    = true;
            chkAll.Enabled      = false;
            btnAbort.Enabled    = false;

            try
            {
                //SetSelectCellStatus(true);
                dgvList.Enabled = false;
                if (btnDownload.Text.ToUpper().EndsWith("DOWNLOAD"))
                {
                    lblStatus.Text = "Saving the download list, please wait...";
                    Application.DoEvents();
                    try
                    {
                        if (callbyPause == false)
                        {
                            DB.DeleteAllUnfinishedDownloadItem(sitename);
                            foreach (DataGridViewRow dgvr in dgvList.Rows)
                            {
                                if ((bool)dgvr.Cells[1].Value == true)
                                {
                                    if (dgvr.Cells["Status"].Value.ToString() == "" || dgvr.Cells["Status"].Value.ToString().StartsWith("<!>"))
                                    {
                                        DB.SaveDownloadlist(sitename, dgvr.Cells["FtpPath"].Value.ToString(), dgvr.Cells["FileName"].Value.ToString(),
                                                            lblLocalPath.Text, dgvr.Cells["Type"].Value.ToString(), dgvr.Cells["Size"].Value.ToString(),
                                                            dgvr.Cells["FileFullName"].Value.ToString(), dgvr.Cells["LocalFullFileName"].Value.ToString(), dgvr.Cells["ModifyDate"].Value.ToString());
                                    }
                                }
                                //dgvr.Cells["Status"].Value = "";
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    lblStatus.Text = "Start downloading...";
                    if (callbyPause == false)
                    {
                        lblFinished.Text = "0";
                    }
                    else
                    {
                        callbyPause = false;
                    }

                    //向上滚动DGV
                    int idgvX = 0, idgvY = 0, iCellX = 0, iCellY = 0;
                    idgvX = dgvList.Location.X;
                    idgvY = dgvList.Location.Y;
                    bool b_Move    = false;
                    int  iRowCount = -1;
                    dgvList.FirstDisplayedScrollingRowIndex = 0;

                    for (int i = 0; i < dgvList.Rows.Count; i++)
                    {
                        if (btnPause.Text == "Start")
                        {
                            break;
                        }
                        #region //向上滚动
                        if (b_Move)
                        {
                            dgvList.FirstDisplayedScrollingRowIndex = dgvList.Rows[i].Index - iRowCount;
                        }
                        iCellX = dgvList.GetCellDisplayRectangle(0, dgvList.Rows[i].Index, false).X;
                        iCellY = dgvList.GetCellDisplayRectangle(0, dgvList.Rows[i].Index, false).Y;

                        //如果得到的单元格的Y坐标=0了,说明到底了,要开始往上移动
                        //如果得到的单元格的Y坐标加上单元格高度比控件高度高了,要开始往上移动
                        //一旦开始移动,则每个循环都要移动了
                        //2倍的单元格高度,因为到底后可能出现横向滚动条,多以提早开始移动
                        if (iCellY == 0 || idgvY + iCellY + dgvList.Rows[i].Cells[0].Size.Height + dgvList.Rows[i].Cells[0].Size.Height > idgvY + dgvList.Height)
                        {
                            dgvList.FirstDisplayedScrollingRowIndex = dgvList.Rows[i].Index - iRowCount;
                            iCellY = dgvList.GetCellDisplayRectangle(0, dgvList.Rows[i].Index, false).Y;
                            b_Move = true;
                        }
                        else
                        {
                            if (b_Move == false)
                            {
                                iRowCount++;
                            }
                        }
                        #endregion

                        if ((bool)dgvList.Rows[i].Cells[1].Value == true
                            &&
                            (dgvList.Rows[i].Cells["Status"].Value.ToString().StartsWith("<OK>") == false && dgvList.Rows[i].Cells["Status"].Value.ToString().StartsWith("<ERROR>") == false))//download
                        {
                            try
                            {
                                ResetStructCurrentDownload();
                                currentdownloadinfo.SiteName            = sitename;
                                currentdownloadinfo.FtpPath             = dgvList.Rows[i].Cells["FtpPath"].Value.ToString();
                                currentdownloadinfo.FileNameWithoutPath = dgvList.Rows[i].Cells["FileName"].Value.ToString();
                                currentdownloadinfo.LocalPath           = lblLocalPath.Text;
                                currentdownloadinfo.Type              = dgvList.Rows[i].Cells["Type"].Value.ToString();
                                currentdownloadinfo.FileSize          = Convert.ToInt32(dgvList.Rows[i].Cells["Size"].Value);
                                currentdownloadinfo.LocalFullFileName = dgvList.Rows[i].Cells["LocalFullFileName"].Value.ToString();

                                if (currentdownloadinfo.Type == "File Folder")
                                {
                                    if (Directory.Exists(currentdownloadinfo.LocalFullFileName) == false)
                                    {
                                        Directory.CreateDirectory(currentdownloadinfo.LocalFullFileName);
                                    }
                                    DB.Deletedownloaditem(currentdownloadinfo.SiteName, currentdownloadinfo.FtpPath, currentdownloadinfo.FileNameWithoutPath);
                                    dgvList.Rows[i].Cells[0].Value        = res.ok;
                                    dgvList.Rows[i].Cells["Status"].Value = "<OK>Finished";
                                }
                                else
                                {
                                    if (renamefile == "Y")
                                    {
                                        currentdownloadinfo.CurrentUsedLocalFullFileName = dgvList.Rows[i].Cells["LocalFullFileName"].Value.ToString() + ".tmp";
                                    }
                                    else
                                    {
                                        currentdownloadinfo.CurrentUsedLocalFullFileName = dgvList.Rows[i].Cells["LocalFullFileName"].Value.ToString();
                                    }
                                    dgvList.Rows[i].Cells[0].Value = res.download;

                                    int finishedlength = 0;
                                    if (dgvList.Rows[i].Cells["Status"].Value.ToString() != "")
                                    {
                                        if (File.Exists(currentdownloadinfo.CurrentUsedLocalFullFileName))
                                        {
                                            finishedlength = (int)(new FileInfo(currentdownloadinfo.CurrentUsedLocalFullFileName)).Length;
                                        }
                                    }
                                    string localfilepath = currentdownloadinfo.LocalFullFileName.Substring(0, currentdownloadinfo.LocalFullFileName.Length - currentdownloadinfo.FileNameWithoutPath.Length - 1);
                                    if (Directory.Exists(localfilepath) == false)
                                    {
                                        Directory.CreateDirectory(localfilepath);
                                    }
                                    if (type == "FTP")
                                    {
                                        Ftp.Download(this, i, localfilepath,
                                                     dgvList.Rows[i].Cells["FileName"].Value.ToString(),
                                                     dgvList.Rows[i].Cells["FileFullName"].Value.ToString(), finishedlength);
                                    }
                                    else
                                    {
                                        sFtp.Download(this, i, localfilepath,
                                                      dgvList.Rows[i].Cells["FileName"].Value.ToString(),
                                                      dgvList.Rows[i].Cells["FileFullName"].Value.ToString(), finishedlength);
                                        sFtp = null;
                                        sFtp = new SFTP.SFTP(sitename, siteip, userid, pwd, port, renamefile, "Override");
                                        sFtp.Connect();
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                try
                                {
                                    dgvList.Rows[i].Cells[0].Value        = res.error;
                                    dgvList.Rows[i].Cells["Status"].Value = "<ERROR>" + ex.Message;
                                    DB.Deletedownloaditem(currentdownloadinfo.SiteName, currentdownloadinfo.FtpPath, currentdownloadinfo.FileNameWithoutPath);
                                }
                                catch { }
                            }
                            finally
                            {
                                try
                                {
                                    lblFinished.Text = (Convert.ToInt32(lblFinished.Text) + 1).ToString();
                                }
                                catch { }
                                Application.DoEvents();
                            }
                        }
                    }
                }
            }
            finally
            {
                if (btnPause.Text != "Start")
                {
                    //SetSelectCellStatus(false);
                    dgvList.Enabled     = true;
                    btnPause.Enabled    = false;
                    btnDownload.Enabled = true;
                    chkAll.Enabled      = true;
                    btnAbort.Enabled    = true;
                    MessageBox.Show("Download finished", "Download", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }