- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.Data;
- using System.IO;
- using System.ComponentModel;
- namespace Common
- {
- public class FTPClient
- {
- private string ftpServerIP = String.Empty;
- private string ftpUser = String.Empty;
- private string ftpPassword = String.Empty;
- private string ftpRootURL = String.Empty;
- public FTPClient(string url, string userid,string password)
- {
- this.ftpServerIP = ftp的IP地址;
- this.ftpUser = 用户名;
- this.ftpPassword = 密码;
- this.ftpRootURL = "ftp://" + url + "/";
- }
- /// <summary>
- /// 上传
- /// </summary>
- /// <param name="localFile">本地文件绝对路径</param>
- /// <param name="ftpPath">上传到ftp的路径</param>
- /// <param name="ftpFileName">上传到ftp的文件名</param>
- public bool fileUpload(FileInfo localFile, string ftpPath, string ftpFileName)
- {
- bool success = false;
- FtpWebRequest ftpWebRequest = null;
- FileStream localFileStream = null;
- Stream requestStream = null;
- try
- {
- string uri = ftpRootURL + ftpPath + ftpFileName;
- ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
- ftpWebRequest.Credentials = new NetworkCredential(ftpUser, ftpPassword);
- ftpWebRequest.UseBinary = true;
- ftpWebRequest.KeepAlive = false;
- ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
- ftpWebRequest.ContentLength = localFile.Length;
- int buffLength = 2048;
- byte[] buff = new byte[buffLength];
- int contentLen;
- localFileStream = localFile.OpenRead();
- requestStream = ftpWebRequest.GetRequestStream();
- contentLen = localFileStream.Read(buff, 0, buffLength);
- while (contentLen != 0)
- {
C#对FTP的操作(上传,下载,重命名文件,删除文件,文件存在检查)
最新推荐文章于 2024-08-05 12:17:26 发布
本文档介绍了一个C#类`FTPClient`,用于执行FTP操作,包括上传文件、从FTP服务器下载文件、重命名文件、删除文件以及检查文件是否存在。类中提供了详细的方法实现,如`fileUpload`、`fileDownload`、`fileRename`、`fileDelete`和`fileCheckExist`,通过`FtpWebRequest`进行FTP交互。
摘要由CSDN通过智能技术生成