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; } }
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(); //if (type == "SFTP") //{ // btnPause.Text = "Exit"; //} 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 download btnDownload.Text = "Continue download"; btnDownload.Width = 129; btnDownload.Left = 702; btnPause.Left = 640; lblStatus.Text = "Getting breakpoint resume files list..."; Thread.Sleep(300); Application.DoEvents(); dtAllResumeFiles = DB.GetDownloadBreakpointResumeFileList(sitename); if (dtAllResumeFiles.Rows.Count > 0) { lblLocalPath.Text = dtAllResumeFiles.Rows[0]["LocalPath"].ToString(); } foreach (DataRow dr in dtAllResumeFiles.Rows) { //取得文件列表后,检索本地文件下载情况。 if (dr["Type"].ToString() == "File Folder") { if (Directory.Exists(dr["LocalFullFilePath"].ToString()) == false) { DB.SaveDownloadlist(sitename, dr["FtpPath"].ToString(), dr["FileNameWithoutPath"].ToString(), lblLocalPath.Text, dr["Type"].ToString(), dr["FileSize"].ToString(), dr["FtpFullFilePath"].ToString(), dr["LocalFullFilePath"].ToString(), dr["ModifyDate"].ToString()); dgvList.Rows.Add(res.wait, false, dr["FtpPath"].ToString(), dr["FileNameWithoutPath"].ToString(), dr["FileSize"].ToString(), "<!>Can't find the folder, need recreat", dr["FtpFullFilePath"].ToString(), dr["LocalFullFilePath"].ToString(), dr["Type"].ToString(), dr["Modify Date"].ToString()); } } else { string filename = null; if (renamefile == "Y") { filename = dr["LocalFullFilePath"].ToString() + ".tmp"; } else { filename = dr["LocalFullFilePath"].ToString(); } //查询FTP上的文件大小和现在的上次下载的是否一致,一致则继续下载,不一致则重新下载 using (DataTable dtCheckFtpFileStatus = (type == "FTP")?Ftp.GetFileList(dr["FtpFullFilePath"].ToString()):sFtp.GetFileList(dr["FtpFullFilePath"].ToString())) { if (dtCheckFtpFileStatus == null || dtCheckFtpFileStatus.Rows.Count == 0) { dgvList.Rows.Add(res.error, false, dr["FtpPath"].ToString(), dr["FileNameWithoutPath"].ToString(), dr["FileSize"].ToString(), "<ERROR>Can't find the source file, can't download", dr["FtpFullFilePath"].ToString(), dr["LocalFullFilePath"].ToString(), dr["Type"].ToString(), dr["Modify Date"].ToString()); } else { if (dtCheckFtpFileStatus.Rows[0]["Size"].ToString() != dr["FileSize"].ToString() || dtCheckFtpFileStatus.Rows[0]["Modify Date"].ToString() != dr["ModifyDate"].ToString()) { DB.SaveDownloadlist(sitename, dr["FtpPath"].ToString(), dr["FileNameWithoutPath"].ToString(), lblLocalPath.Text, dr["Type"].ToString(), dtCheckFtpFileStatus.Rows[0]["Size"].ToString(), dr["FtpFullFilePath"].ToString(), dr["LocalFullFilePath"].ToString(), dtCheckFtpFileStatus.Rows[0]["Modify Date"].ToString()); dgvList.Rows.Add(res.wait, false, dr["FtpPath"].ToString(), dr["FileNameWithoutPath"].ToString(), dtCheckFtpFileStatus.Rows[0]["Size"].ToString(), "<!>The source file has been changed, need redownload", dr["FtpFullFilePath"].ToString(), dr["LocalFullFilePath"].ToString(), dr["Type"].ToString(), dtCheckFtpFileStatus.Rows[0]["Modify Date"].ToString()); } else { if (File.Exists(filename)) { FileInfo fi = new FileInfo(filename); //fi的长度就是offset dgvList.Rows.Add(res.wait, false, dr["FtpPath"].ToString(), dr["FileNameWithoutPath"].ToString(), dr["FileSize"].ToString(), "<!>Download finished " + Convert.ToInt32((Convert.ToDouble(fi.Length) / Convert.ToDouble(dr["FileSize"].ToString())) * 100).ToString() + "%", dr["FtpFullFilePath"].ToString(), dr["LocalFullFilePath"].ToString(), dr["Type"].ToString(), dr["ModifyDate"].ToString()); } else { DB.SaveDownloadlist(sitename, dr["FtpPath"].ToString(), dr["FileNameWithoutPath"].ToString(), lblLocalPath.Text, dr["Type"].ToString(), dr["FileSize"].ToString(), dr["FtpFullFilePath"].ToString(), dr["LocalFullFilePath"].ToString(), dr["ModifyDate"].ToString()); dgvList.Rows.Add(res.wait, false, dr["FtpPath"].ToString(), dr["FileNameWithoutPath"].ToString(), dr["FileSize"].ToString(), "<!>Can't find the last download file, need redownload", dr["FtpFullFilePath"].ToString(), dr["LocalFullFilePath"].ToString(), dr["Type"].ToString(), dr["ModifyDate"].ToString()); } } } } } } #endregion } else { lblStatus.Text = "Getting files list for download, please wait..."; Thread.Sleep(300); Application.DoEvents(); #region read files and folders foreach (DataRow dr in dtChoosedList.Rows) { string ftppath = dr["FtpPath"].ToString(); ftppath = ftppath.Substring(sitename.Length, ftppath.Length - sitename.Length); while (ftppath.StartsWith("/")) { ftppath = ftppath.Substring(1, ftppath.Length - 1); } int ilast; string tmpremotefolder; if (ftppath.EndsWith("/")) { tmpremotefolder = ftppath.Substring(0, ftppath.Length - 1); } else { tmpremotefolder = ftppath; } //本地需要建立下载目录同名的目录,就是将选中的FTP文件夹在本地建立,而不只是下载FTP文件夹里面的内容 ilast = tmpremotefolder.LastIndexOf("/"); string ipath = ftppath.Substring(ilast + 1, tmpremotefolder.Length - ilast - 1); string localfolder = lblLocalPath.Text + "\\" + ipath; if (dr["IsFolder"].ToString() == "Y") { DoWhileRemoteIsFolder(ftppath, localfolder); } else { //是文件的,要把文件名从路径中去掉,显示在界面上 DataRow drFile = dtAllFiles.NewRow(); if (ftppath.LastIndexOf("/") < 0) { drFile["FtpPath"] = "/"; drFile["File Name"] = ftppath; } else { drFile["FtpPath"] = ftppath.Substring(0, ftppath.LastIndexOf("/")); drFile["File Name"] = ftppath.Substring(ftppath.LastIndexOf("/") + 1, ftppath.Length - ftppath.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["LocalFullFilePath"] = localfolder;//文件夹第一层这个路径就是文件路径了,不用再加文件名 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["FtpPath"].ToString(), drallfile["File Name"].ToString(), drallfile["Size"].ToString(), "", drallfile["FileFullPath"].ToString(), drallfile["LocalFullFilePath"].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 { btnDownload.Enabled = true; this.Cursor = Cursors.Default; } }