/// <summary> /// 列出服务服务器上指定目录下的所有文件 /// </summary> /// <param name="directoryPath"></param> /// <returns></returns> public string[] ListFiles(string directoryPath) { List <string> filesList = new List <string>(); OpenResponse(directoryPath, WebRequestMethods.Ftp.ListDirectory); using (StreamReader streamReader = new StreamReader(response.GetResponseStream())) { return(Gadget.SplitString(streamReader.ReadToEnd(), Constant.TextConstant.FtpNewLine)); } }
/// <summary> /// 上传文件到服务器 /// </summary> /// <param name="fileBytes">上传文件的字节流</param> /// <param name="remoteFileName">上传文件后的重命名</param> /// <param name="overWriteFile">是否重写服务器上的文件</param> public void UploadFile(byte[] fileBytes, string remoteFileName, bool overWriteFile) { OpenResponse(overWriteFile ? remoteFileName : Gadget.ReturnFileNameWithCurrentDate(remoteFileName), WebRequestMethods.Ftp.UploadFile); using (Stream stream = request.GetRequestStream()) { using (MemoryStream memoryStream = new MemoryStream(fileBytes)) { byte[] buffer = new byte[Constant.FTP.LenOfBuffer]; int bytesRead = 0; int totalRead = 0; while (true) { bytesRead = memoryStream.Read(buffer, 0, buffer.Length); if (bytesRead == 0) { break; } totalRead += bytesRead; stream.Write(buffer, 0, bytesRead); } } response = request.GetResponse() as FtpWebResponse; } }
/// <summary> /// 下载服务器文件到本地 /// </summary> /// <param name="remoteFileName">要下载的服务器上的文件名</param> /// <param name="localPath">下载到本地的路径</param> /// <param name="localFileName">下载到本地的文件的重命名</param> /// <param name="overWriteFile">是否要重写本地的文件</param> public void DownloadFile(string remoteFileName, string localPath, string localFileName, bool overWriteFile) { byte[] fileBytes = DownloadFile(remoteFileName); if (fileBytes != null) { using (FileStream fileStream = new FileStream(Path.Combine(localPath, overWriteFile ? localFileName : Gadget.ReturnFileNameWithCurrentDate(localFileName)), FileMode.Create)) { fileStream.Write(fileBytes, 0, fileBytes.Length); fileStream.Flush(); } } }