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

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;
            }
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值