C# FTP 文件 文件夹操作 上传 下载

时间:2021-03-18 21:00:02

C# FTP 文件 文件夹操作 上传 下载

 

最近研究了用代码操作FTP的文件和文件夹操作

得到FTP的连接:

            FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(path);
            //指定数据传输类型   false 文件类型, true 二进制;类型
            reqFTP.UseBinary = true;
            //ftp用户名和密码
            reqFTP.Credentials = new NetworkCredential(ftpUserId, ftpPassword);

文件上传

         /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="filename"></param>
        public void Upload(string filename,string directoryName)
        {
            FileInfo fileInf = new FileInfo(filename);
            string uri = "ftp://" + ftpServerIP + "//" + directoryName+"//"+fileInf.Name;

            //得到连接
            Connect(uri);
            //默认为true,连接不会被关闭
            //在一个命令之后被执行
            reqFTP.KeepAlive = false;
            //指定执行命令
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            //指定文件的大小
            reqFTP.ContentLength = fileInf.Length;
            //缓存大小设置为kb
            int bufflength = 2048;
            byte[] buff = new byte[bufflength];
            int contenLen;
            //打开一个文件流(System.IO.FileString)去读上传文件
            FileStream fs = fileInf.OpenRead();
            try
            {
                //把上传的文件写入流
                Stream stream = reqFTP.GetRequestStream();
                //每次读文件流的kb
                contenLen = fs.Read(buff,0,bufflength);
                //流内容没有结束
                while (contenLen != 0)
                {
                    stream.Write(buff,0,contenLen);
                    contenLen = fs.Read(buff,0,bufflength);
                }
                //关闭两个流
                stream.Close();
                fs.Close();
            }
            catch (Exception e)
            {

                MessageBox.Show("upload error", e.Message);
            }
        }

 

 /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        /// <param name="errorinfo"></param>
        /// <returns></returns>
        public bool Downlad(string filePath, string fileName)
        {
            try
            {
                string onlyFileName = Path.GetFileName(fileName);
                string newFileName = filePath + "//" + onlyFileName;
                if (File.Exists(newFileName))
                {
                    //errorinfo = string.Format("本地文件{0}已存在,无法下载", newFileName);
                    return false;
                }

                string url = "ftp://" + ftpServerIP + "/" + fileName;
                //取得连接
                Connect(url);
                reqFTP.Credentials = new NetworkCredential(ftpUserId,ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];

                readCount = ftpStream.Read(buffer,0,bufferSize);
                FileStream outputStream = new FileStream(newFileName,FileMode.Create);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0,bufferSize);
                }
                ftpStream.Close();
                outputStream.Close();
                response.Close();
                //errorinfo = "";
                return true;
            }
            catch (Exception ex)
            {
                //errorinfo = string.Format("因{0},无法下载", ex.Message);

                MessageBox.Show("download错误",ex.Message);

                return false;
            }
        }