Пример #1
0
 /// <summary>
 /// 初始化
 /// </summary>
 public static void InitFtp(string ip, string user, string pwd)
 {
     if (!_IsFtpInit)
     {
         try
         {
             _Url = "ftp://" + ip + "/";
             IP   = ip;
             User = user;
             Pwd  = pwd;
             if (!Directory.Exists("./Tasks"))
             {
                 Directory.CreateDirectory("Tasks");
             }
             if (!Directory.Exists("./Tasks/UploadDirectory"))
             {
                 Directory.CreateDirectory("./Tasks/UploadDirectory");
             }
             if (!Directory.Exists("./Tasks/UploadFile"))
             {
                 Directory.CreateDirectory("./Tasks/UploadFile");
             }
             if (!Directory.Exists("./Tasks/DownloadDirectory"))
             {
                 Directory.CreateDirectory("./Tasks/DownloadDirectory");
             }
             if (!Directory.Exists("./Tasks/DownloadFile"))
             {
                 Directory.CreateDirectory("./Tasks/DownloadFile");
             }
             //读取未完成的上传、下载任务列表
             List <string> paths = FileHelper.GetFiles("./Tasks/UploadDirectory");
             if (paths != null)
             {
                 foreach (string item in paths)
                 {
                     FtpUploadDirectoryInfoList.Add(FileHelper.Deserialize <FileInfoEntity>(item));
                 }
             }
             paths = FileHelper.GetFiles("./Tasks/UploadFile");
             if (paths != null)
             {
                 foreach (string item in paths)
                 {
                     FtpUploadFileInfoList.Add(FileHelper.Deserialize <FileInfoEntity>(item));
                 }
             }
             paths = FileHelper.GetFiles("./Tasks/DownloadDirectory");
             if (paths != null)
             {
                 foreach (string item in paths)
                 {
                     FtpDownloadDirectoryInfoList.Add(FileHelper.Deserialize <FileInfoEntity>(item));
                 }
             }
             paths = FileHelper.GetFiles("./Tasks/DownloadFile");
             if (paths != null)
             {
                 foreach (string item in paths)
                 {
                     FtpDownloadFileInfoList.Add(FileHelper.Deserialize <FileInfoEntity>(item));
                 }
             }
             //删除过期任务
             for (int i = 0; i < FtpUploadFileInfoList.Count(); i++)
             {
                 if (FtpUploadFileInfoList[i].ResultCode == ResultCode.Failed || FtpUploadFileInfoList[i].ResultCode == ResultCode.Cancelled)
                 {
                     FileHelper.DeleteFile("./Tasks/UploadFile/" + FtpUploadFileInfoList[i].FileNo + ".dat");
                     FtpUploadFileInfoList.Remove(FtpUploadFileInfoList[i]);
                 }
             }
             for (int i = 0; i < FtpDownloadFileInfoList.Count(); i++)
             {
                 if (FtpDownloadFileInfoList[i].ResultCode == ResultCode.Failed || FtpDownloadFileInfoList[i].ResultCode == ResultCode.Cancelled)
                 {
                     FileHelper.DeleteFile("./Tasks/DownloadFile/" + FtpDownloadFileInfoList[i].FileNo + ".dat");
                     FtpDownloadFileInfoList.Remove(FtpDownloadFileInfoList[i]);
                 }
             }
             for (int i = 0; i < FtpUploadDirectoryInfoList.Count(); i++)
             {
                 var query = (from x in FtpUploadFileInfoList
                              where x.OperationNo == FtpUploadDirectoryInfoList[i].OperationNo
                              select x).FirstOrDefault();
                 if (query == null)
                 {
                     FileHelper.DeleteFile("./Tasks/UploadDirectory/" + FtpUploadDirectoryInfoList[i].FileNo + ".dat");
                     FtpUploadDirectoryInfoList.Remove(FtpUploadDirectoryInfoList[i]);
                 }
             }
             for (int i = 0; i < FtpDownloadDirectoryInfoList.Count(); i++)
             {
                 var query = (from x in FtpDownloadFileInfoList
                              where x.OperationNo == FtpDownloadDirectoryInfoList[i].OperationNo
                              select x).FirstOrDefault();
                 if (query == null)
                 {
                     FileHelper.DeleteFile("./Tasks/DownloadDirectory/" + FtpDownloadDirectoryInfoList[i].FileNo + ".dat");
                     FtpDownloadDirectoryInfoList.Remove(FtpDownloadDirectoryInfoList[i]);
                 }
             }
         }
         catch
         {
             //nothing
         }
         finally
         {
             _IsFtpInit = true;
         }
     }
 }
Пример #2
0
 /// <summary>
 /// 上传
 /// </summary>
 private void Upload(FileInfoEntity info)
 {
     if (info == null)
     {
         return;
     }
     try
     {
         //判断网络是否异常
         if (!CheckLinkState(_Url))
         {
             info.ResultCode = ResultCode.NetworkError;
             throw new Exception();
         }
         //判断源文件是否更新或者移除
         if (!File.Exists(info.FilePath + "/" + info.FileName) || info.Length != FileHelper.GetFileLength(info.FilePath + "/" + info.FileName))
         {
             info.ResultCode = ResultCode.SourceError;
             throw new Exception();
         }
         //判断目标目录是否被移除
         if (!CheckExistOfRemoteDirectory(_Url + info.NewFilePath))
         {
             info.ResultCode = ResultCode.TargetError;
             throw new Exception();
         }
         //启动上传通知事件
         if (Started != null)
         {
             Started(this, new FileEventArgs(info, null));
         }
         //执行任务
         InnerUploadFile(info);
         //再次确认远程文件是否有重名旧文件,有就删除旧文件
         if (CheckExistOfRemoteFile(_Url + (info.NewFilePath == "" ? "" : info.NewFilePath + "/") + info.NewFileName))
         {
             //移除
             DeleteRemoteFile(_Url + (info.NewFilePath == "" ? "" : info.NewFilePath + "/") + info.NewFileName);
         }
         //将临时文件名改名
         RenameRemoteFile(_Url + (info.NewFilePath == "" ? "" : info.NewFilePath + "/"), info.NewFileName + ".tmp", info.NewFileName);
         //完成
         info.ResultCode             = ResultCode.Done;
         info.OperationResultMessage = info.Method.ToString() + " " + info.FileType.ToString() + " " + info.ResultCode.ToString();
         FileHelper.DeleteFile("./Tasks/UploadFile/" + info.FileNo + ".dat");
         FtpUploadFileInfoList.Remove(info);
         if (Done != null)
         {
             Done(this, new FileEventArgs(info, null));
         }
     }
     catch
     {
         //任务失败
         if (info.ResultCode == ResultCode.Cancelled || info.ResultCode == ResultCode.Failed || info.ResultCode == ResultCode.SourceError || info.ResultCode == ResultCode.TargetError)
         {
             info.OperationResultMessage = info.Method.ToString() + " " + info.FileType.ToString() + " " + info.ResultCode.ToString();
             Task.Factory.StartNew(() =>
             {
                 FtpHelper.DeleteRemoteFile(_Url + (info.NewFilePath == "" ? "" : info.NewFilePath + "/") + info.NewFileName + ".tmp");
             });
             FileHelper.DeleteFile("./Tasks/UploadFile/" + info.FileNo + ".dat");
             FtpUploadFileInfoList.Remove(info);
             if (info.ResultCode == ResultCode.Cancelled)
             {
                 if (Cancelled != null)
                 {
                     Cancelled(this, new FileEventArgs(info, new Exception(info.OperationResultMessage)
                     {
                     }));
                 }
             }
             else
             {
                 if (Failed != null)
                 {
                     Failed(this, new FileEventArgs(info, new Exception(info.OperationResultMessage)
                     {
                     }));
                 }
             }
         }
         //任务暂停
         else
         {
             info.OperationResultMessage = info.Method.ToString() + " " + info.FileType.ToString() + " " + info.ResultCode.ToString();
             if (Paused != null)
             {
                 Paused(this, new FileEventArgs(info, new Exception(info.OperationResultMessage)
                 {
                 }));
             }
         }
     }
     finally
     {
         //nothing
     }
 }