private void t_prepare_Tick(object sender, EventArgs e) { try { t_prepare.Enabled = false; this.Cursor = Cursors.AppStarting; clickbychkall = true; //在读取文件的时候读取空文件夹,因为读取文件列表的时候空文件夹不在文件列表中,会造成空文件夹上传丢失 lblStatus.Text = "Getting site profile..."; Application.DoEvents(); using (DataTable dt = DB.GetSiteProfileDetail(sitename)) { type = dt.Rows[0]["Type"].ToString(); siteip = dt.Rows[0]["SiteIP"].ToString(); userid = dt.Rows[0]["UserID"].ToString(); pwd = Security.Decrypt(dt.Rows[0]["Password"].ToString()); renamefile = dt.Rows[0]["RenameFile"].ToString(); port = dt.Rows[0]["Port"].ToString(); } lblStatus.Text = "Creating " + type + " instance..."; if (type == "FTP") { Ftp = new FTP.FTP(sitename, siteip, userid, pwd, port, renamefile, "Override"); } else { sFtp = new SFTP.SFTP(sitename, siteip, userid, pwd, port, renamefile, "Override"); sFtp.Connect(); } Thread.Sleep(300); Application.DoEvents(); if (dtChoosedList == null) { #region Continue upload btnUpload.Text = "Continue upload"; btnUpload.Width = 129; btnUpload.Left = 702; btnPause.Left = 640; lblStatus.Text = "Getting breakpoint resume files list..."; Thread.Sleep(300); Application.DoEvents(); dtAllResumeFiles = DB.GetUploadBreakpointResumeFileList(sitename); if (dtAllResumeFiles.Rows.Count > 0) { lblFtpPath.Text = dtAllResumeFiles.Rows[0]["FtpPath"].ToString(); } foreach (DataRow dr in dtAllResumeFiles.Rows) { //取得文件列表后,检索服务器上文件上传载情况。 if (dr["Type"].ToString() == "File Folder") { bool bChkResult = true; if (type == "FTP") { bChkResult = Ftp.CheckFileExist(dr["FtpFullFilePath"].ToString(), dr["FileNameWithoutPath"].ToString()); } else { bChkResult = sFtp.CheckFileExist(dr["FtpFullFilePath"].ToString(), dr["FileNameWithoutPath"].ToString()); } if (bChkResult == false) { DB.SaveUploadlist(sitename, dr["LocalPath"].ToString(), dr["FileNameWithoutPath"].ToString(), lblFtpPath.Text, dr["Type"].ToString(), dr["FileSize"].ToString(), dr["LocalFullFilePath"].ToString(), dr["FtpFullFilePath"].ToString(), dr["ModifyDate"].ToString()); dgvList.Rows.Add(res.wait, false, dr["LocalPath"].ToString(), dr["FileNameWithoutPath"].ToString(), dr["FileSize"].ToString(), "<!>Can't find the folder, need recreat", dr["LocalFullFilePath"].ToString(), dr["FtpFullFilePath"].ToString(), dr["Type"].ToString(), dr["ModifyDate"].ToString()); } } else { string filename = null; if (renamefile == "Y") { filename = dr["FtpFullFilePath"].ToString() + ".tmp"; } else { filename = dr["FtpFullFilePath"].ToString(); } //查询FTP上的文件大小和现在的上次下载的是否一致,一致则继续下载,不一致则重新下载 if (File.Exists(dr["LocalFullFilePath"].ToString()) == false) { dgvList.Rows.Add(res.error, false, dr["LocalPath"].ToString(), dr["FileNameWithoutPath"].ToString(), dr["FileSize"].ToString(), "<ERROR>Can't find the source file, can't download", dr["LocalFullFilePath"].ToString(), dr["FtpFullFilePath"].ToString(), dr["Type"].ToString(), dr["ModifyDate"].ToString()); } else { FileInfo currlocalfileinfo = new FileInfo(dr["LocalFullFilePath"].ToString()); if (currlocalfileinfo.Length.ToString() != dr["FileSize"].ToString() || currlocalfileinfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss") != dr["ModifyDate"].ToString()) { DB.SaveUploadlist(sitename, dr["LocalPath"].ToString(), dr["FileNameWithoutPath"].ToString(), lblFtpPath.Text, dr["Type"].ToString(), currlocalfileinfo.Length.ToString(), dr["LocalFullFilePath"].ToString(), dr["FtpFullFilePath"].ToString(), currlocalfileinfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")); dgvList.Rows.Add(res.wait, false, dr["LocalPath"].ToString(), dr["FileNameWithoutPath"].ToString(), currlocalfileinfo.Length.ToString(), "<!>The source file has been changed, need reupload", dr["LocalFullFilePath"].ToString(), dr["FtpFullFilePath"].ToString(), dr["Type"].ToString(), currlocalfileinfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")); } else { DataTable dtFtpFileInfo = (type == "FTP") ? Ftp.GetFileList(filename) : sFtp.GetFileList(filename); if (dtFtpFileInfo != null && dtFtpFileInfo.Rows.Count > 0) { //fi的长度就是offset dgvList.Rows.Add(res.wait, false, dr["LocalPath"].ToString(), dr["FileNameWithoutPath"].ToString(), dr["FileSize"].ToString(), "<!>Upload finished " + Convert.ToInt32((Convert.ToDouble(dtFtpFileInfo.Rows[0]["Size"].ToString()) / Convert.ToDouble(currlocalfileinfo.Length)) * 100).ToString() + "%", dr["LocalFullFilePath"].ToString(), dr["FtpFullFilePath"].ToString(), dr["Type"].ToString(), dr["ModifyDate"].ToString()); } else { DB.SaveUploadlist(sitename, dr["LocalPath"].ToString(), dr["FileNameWithoutPath"].ToString(), lblFtpPath.Text, dr["Type"].ToString(), dr["FileSize"].ToString(), dr["LocalFullFilePath"].ToString(), dr["FtpFullFilePath"].ToString(), dr["ModifyDate"].ToString()); dgvList.Rows.Add(res.wait, false, dr["LocalPath"].ToString(), dr["FileNameWithoutPath"].ToString(), dr["FileSize"].ToString(), "<!>Can't find the last upload file, need reupload", dr["LocalFullFilePath"].ToString(), dr["FtpFullFilePath"].ToString(), dr["Type"].ToString(), dr["ModifyDate"].ToString()); } } } } } #endregion } else { lblStatus.Text = "Getting files list for upload, please wait..."; Thread.Sleep(300); Application.DoEvents(); #region read files and folders foreach (DataRow dr in dtChoosedList.Rows) { string localpath = dr["LocalPath"].ToString(); if (localpath.EndsWith("\\")) { localpath = localpath.Substring(0, localpath.Length - 1); } if (dr["IsFolder"].ToString() == "Y") { string ipath = localpath.Substring(0, localpath.LastIndexOf("\\")); //try //{ // ftp.MakeFolder(LocalFileFolder, RemoteFileFolder, ipath);//如果源是文件夹,则连源文件夹一起上传 //} //catch { } DoWhileLocalIsFolder(ftppathwithoutip, localpath, initiallocalpath); } else { //只上传指定文件 //ftp.Upload(o, iRow, RemoteFileFolder, LocalFileFolder, truelocalfilefolder); //是文件的,要把文件名从路径中去掉,显示在界面上 DataRow drFile = dtAllFiles.NewRow(); if (localpath.LastIndexOf("\\") < 0) { drFile["LocalPath"] = ""; drFile["File Name"] = localpath; } else { drFile["LocalPath"] = localpath.Substring(0, localpath.LastIndexOf("\\")); drFile["File Name"] = localpath.Substring(localpath.LastIndexOf("\\") + 1, localpath.Length - localpath.LastIndexOf("\\") - 1); } drFile["Size"] = dr["Size"].ToString(); drFile["Type"] = dr["Type"].ToString(); drFile["Modify Date"] = dr["Modify Date"].ToString(); drFile["FileFullPath"] = dr["FileFullPath"].ToString(); drFile["IsFolder"] = "N"; drFile["FtpFullFilePath"] = GetFtpFullPath(new FileInfo(dr["FileFullPath"].ToString()), null, initiallocalpath, ftppathwithoutip); //文件夹第一层这个路径就是文件路径了,不用再加文件名 dtAllFiles.Rows.Add(drFile); } } #endregion foreach (DataRow drallfile in dtAllFiles.Rows) { DataGridViewCheckBoxColumn dgvcc = new DataGridViewCheckBoxColumn(); dgvcc.HeaderText = ""; dgvcc.Resizable = DataGridViewTriState.False; dgvList.Rows.Add(res.wait, false, drallfile["LocalPath"].ToString(), drallfile["File Name"].ToString(), drallfile["Size"].ToString(), "", drallfile["FileFullPath"].ToString(), drallfile["FtpFullFilePath"].ToString(), drallfile["Type"].ToString(), drallfile["Modify Date"].ToString()); } } dgvList.ClearSelection(); chkAll.Checked = true; lblTotal.Text = dgvList.Rows.Count.ToString(); lblStatus.Text = "Get files list finished"; } catch (Exception ex) { MessageBox.Show(ex.Message, "Download", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { btnUpload.Enabled = true; this.Cursor = Cursors.Default; } }