C# ftp 上传、下载、删除

时间:2025-04-06 20:38:59
 public class FtpHelper
    {
        public static readonly FtpHelper Instance = new FtpHelper();

        /// <summary>
        /// 取得文件名
        /// </summary>
        /// <param name="ftpPath">ftp路径</param>
        /// <returns></returns>
        public string[] GetFilePath(string userId, string pwd, string ftpPath)
        {
            string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            FtpWebRequest reqFTP;
            try
            {
                reqFTP = (FtpWebRequest)(new Uri(ftpPath));
                 = true;
                 = new NetworkCredential(userId, pwd);
                 = ;
                 = false;
                WebResponse response = ();
                StreamReader reader = new StreamReader(());
                string line = ();
                while (line != null)
                {
                    (line);
                    ("\n");
                    line = ();
                }
                (().LastIndexOf('\n'), 1);
                ();
                ();
                return ().Split('\n');
            }
            catch (Exception ex)
            {
                downloadFiles = null;
                return downloadFiles;
            }
        }

        //ftp的上传功能
        public void Upload(string userId, string pwd, string filename, string ftpPath)
        {
            FileInfo fileInf = new FileInfo(filename);
            FtpWebRequest reqFTP;
            // 根据uri创建FtpWebRequest对象 
            reqFTP = (FtpWebRequest)(new Uri(ftpPath + ));
            // ftp用户名和密码
             = new NetworkCredential(userId, pwd);

             = false;
            // 默认为true,连接不会被关闭
            // 在一个命令之后被执行
             = false;
            // 指定执行什么命令
             = ;
            // 指定数据传输类型
             = true;
            // 上传文件时通知服务器文件的大小
             = ;
            // 缓冲大小设置为2kb
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;
            // 打开一个文件流 () 去读上传的文件
            FileStream fs = ();
            try
            {
                // 把上传的文件写入流
                Stream strm = ();
                // 每次读文件流的2kb
                contentLen = (buff, 0, buffLength);
                // 流内容没有结束
                while (contentLen != 0)
                {
                    // 把内容从file stream 写入 upload stream
                    (buff, 0, contentLen);
                    contentLen = (buff, 0, buffLength);
                }
                // 关闭两个流
                ();
                ();
            }
            catch (Exception ex)
            {

            }
        }

        public void Delete(string userId, string pwd, string ftpPath, string fileName)
        {
            FtpWebRequest reqFTP;
            try
            {
                reqFTP = (FtpWebRequest)(new Uri(ftpPath + fileName));
                 = ;
                 = true;
                 = new NetworkCredential(userId, pwd);
                 = false;
                FtpWebResponse listResponse = (FtpWebResponse)();
                string sStatus = ;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //从ftp服务器上下载文件的功能
        public void Download(string userId, string pwd, string ftpPath, string filePath, string fileName)
        {
            FtpWebRequest reqFTP;
            try
            {
                FileStream outputStream = new FileStream(filePath + "\\" + fileName, );
                reqFTP = (FtpWebRequest)(new Uri(ftpPath + fileName));
                 = ;
                 = true;
                 = new NetworkCredential(userId, pwd);
                 = false;
                FtpWebResponse response = (FtpWebResponse)();
                Stream ftpStream = ();
                long cl = ;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
                readCount = (buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    (buffer, 0, readCount);
                    readCount = (buffer, 0, bufferSize);
                }
                ();
                ();
                ();


            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }