void _sftpserver_OnFileOpen(object sender, SftpserverFileOpenEventArgs e) { string operation = ""; if ((e.Flags & 1) != 0) //Read { operation = "downloading"; //如果fileopen状态为没有发现文件则认为这是下载并且文件也确实没有,需要把otherdirfileinfo的状态清空 if (e.StatusCode == 2) { if (m_filepath.Contains(e.ConnectionId)) { OtherDirFileInfo odfi = m_filepath[e.ConnectionId] as OtherDirFileInfo; if (!odfi.m_is_download) { //还需要删除多余目录 odfi.clean(); } } } } if ((e.Flags & 2) != 0) //Write { operation = "uploading"; } LogHelper.InfoLog(e.User + " started " + operation + " " + e.Path + " " + e.Flags); }
void _sftpserver_OnFileClose(object sender, SftpserverFileCloseEventArgs e) { LogHelper.InfoLog(e.User + " transferred " + e.Path); OtherDirFileInfo ofi = m_filepath[e.ConnectionId] as OtherDirFileInfo; if (ofi != null) { if (ofi.m_is_download && !string.IsNullOrEmpty(ofi.m_filePath)) { try { LogHelper.InfoLog(string.Format("file download success,delete file:{0}", ofi.m_filePath)); File.Delete(ofi.m_filePath); } catch (System.Exception ex) { LogHelper.ErrorLog(ex); } } else { Parameters pars = Parameters.Instance(); string scr_path = ""; if (ofi.m_filePath.IndexOf(".vls") != -1) { scr_path = pars.SftpParam.ScrDriver + e.Path; } else { scr_path = pars.SftpParam.VoxDriver + e.Path; } scr_path = scr_path.Replace("/", @"\"); try { //如果没有原路径代表可能此文件之前为获取文件大小 if (string.IsNullOrEmpty(ofi.m_filePath)) { File.Delete(pars.SftpParam.RootDir + e.Path); LogHelper.InfoLog(string.Format("file download success,delete file:{0}sp", ofi.m_filePath)); } else { Common.CopyFileEx(ofi.m_filePath, scr_path, true); File.Delete(ofi.m_filePath); LogHelper.InfoLog(string.Format("file upload success,copye file:{0},delete file:{1};", scr_path, ofi.m_filePath)); } } catch (System.Exception ex) { LogHelper.ErrorLog(ex); } } ofi.clean(); } }