FTP基本操作类大全,外加c#基础公共帮助类

     总结平时用到的一些FTP操作类,方便需要的用到。github地址:

     /// <summary>          /// 连接FTP服务器         /// </summary>          /// <param name="FtpServerIP">FTP连接地址</param>          /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>          /// <param name="FtpUserID">用户名</param>          /// <param name="FtpPassword">密码</param>          public FTPHelper(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)         {             ftpServerIP = FtpServerIP;             ftpRemotePath = FtpRemotePath;             ftpUserID = FtpUserID;             ftpPassword = FtpPassword;             ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";         }
复制代码

2、上传

复制代码
     /// <summary>          /// 上传           /// </summary>           public void Upload(string filename)         {             FileInfo fileInf = new FileInfo(filename);             FtpWebRequest reqFTP;             reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileInf.Name));             reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);             reqFTP.Method = WebRequestMethods.Ftp.UploadFile;             reqFTP.KeepAlive = false;             reqFTP.UseBinary = true;             reqFTP.ContentLength = fileInf.Length;             int buffLength = 2048;             byte[] buff = new byte[buffLength];             int contentLen;             FileStream fs = fileInf.OpenRead();             try             {                 Stream strm = reqFTP.GetRequestStream();                 contentLen = fs.Read(buff, 0, buffLength);                 while (contentLen != 0)                 {                     strm.Write(buff, 0, contentLen);                     contentLen = fs.Read(buff, 0, buffLength);                 }                 strm.Close();                 fs.Close();             }             catch (Exception ex)             {                 throw new Exception(ex.Message);             }         }
复制代码

3、下载

复制代码
    /// <summary>          /// 下载           /// </summary>           public void Download(string filePath, string fileName)         {             try             {                 FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);                 FtpWebRequest reqFTP;                 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));                 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);                 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;                 reqFTP.UseBinary = true;                 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();                 Stream ftpStream = response.GetResponseStream();                 long cl = response.ContentLength;                 int bufferSize = 2048
                        
关键字:
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信