c# ftp client 上传、下载与删除

 

    简单介绍一下ftp client 上传、下载与删除,这是目前比较常用的命令,各个方法其实都差不多,重点是了解ftp命令协议。

1.建立连接

        public static string connect(string path, string login, string password)
        {
            try
            {
                // 根据uri创建ftpwebrequest对象
                reqftp = (ftpwebrequest)webrequest.create(new uri(path));

                //指定命令
                reqftp.method = webrequestmethods.ftp.listdirectory;

                // 指定数据传输类型
                reqftp.usebinary = true;

                // ftp用户名和密码
                reqftp.credentials = new networkcredential(login, password);

                //
                ftpwebresponse response = (ftpwebresponse)reqftp.getresponse();

                return "ftp连接成功";
            }
            catch(exception ex)
            {
                return "ftp连接失败," + ex.message;
            }
            
        }

2.上传文件

        public static string uploadfile(string filename, string ftppath, string login, string password)
        {
            try

            {
                fileinfo fileinf = new fileinfo(filename);

                //判断是否有上级目录

                string uri = "ftp://" + path.combine(ftppath, fileinf.name);

                // 根据uri创建ftpwebrequest对象
                reqftp = (ftpwebrequest)webrequest.create(new uri(uri));

                // 指定数据传输类型
                reqftp.usebinary = true;

                // ftp用户名和密码
                reqftp.credentials = new networkcredential(login, password);

                // 默认为true,连接不会被关闭

                // 在一个命令之后被执行

                reqftp.keepalive = false;

                // 指定执行什么命令

                reqftp.method = webrequestmethods.ftp.uploadfile;

                // 上传文件时通知服务器文件的大小

                reqftp.contentlength = fileinf.length;

                // 缓冲大小设置为kb 

                int bufflength = 2048;

                byte[] buff = new byte[bufflength];

                int contentlen;

                // 打开一个文件流(system.io.filestream) 去读上传的文件

                filestream fs = fileinf.openread();



                // 把上传的文件写入流

                stream strm = reqftp.getrequeststream();

                // 每次读文件流的kb 

                contentlen = fs.read(buff, 0, bufflength);

                // 流内容没有结束

                while (contentlen != 0)

                {

                    // 把内容从file stream 写入upload stream 

                    strm.write(buff, 0, contentlen);

                    contentlen = fs.read(buff, 0, bufflength);

                }

                // 关闭两个流

                strm.close();

                fs.close();
                //successinfo
                return string.format("本地文件{0}已成功上传", fileinf.name);
            }

            catch (exception ex)

            {
                //errorinfo
                return "上传失败" + ex.message;
            }

        }

3.下载文件

        public static string downloadfile(string filedownpath, string filename, string ftppath, string login, string password)
        {
            try
            {
                string onlyfilename = path.getfilename(filename);

                string newfilename = filedownpath + onlyfilename;

                if (file.exists(newfilename))

                {

                    string errorinfo = string.format("文件{0}在该目录下已存在,无法下载", filename);

                    return errorinfo;
                }

                string uri = "ftp://" + path.combine(ftppath, filename);

                // 根据uri创建ftpwebrequest对象
                reqftp = (ftpwebrequest)webrequest.create(new uri(uri));

                // 指定数据传输类型
                reqftp.usebinary = true;

                // ftp用户名和密码
                reqftp.credentials = new networkcredential(login, password);

                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();

                //successinfo

                return string.format("服务器文件{0}已成功下载", filename);

            }

            catch (exception ex)

            {
                //errorinfo
                return string.format("因{0},无法下载", ex.message);

            }

        }

4.删除文件

        public static string deletefile(string filename, string ftppath, string login, string password)
        {
            try
            {
                fileinfo fileinf = new fileinfo(filename);

                string uri = "ftp://" + path.combine(ftppath, fileinf.name);

                // 根据uri创建ftpwebrequest对象
                reqftp = (ftpwebrequest)webrequest.create(new uri(uri));

                // 指定数据传输类型
                reqftp.usebinary = true;

                // ftp用户名和密码
                reqftp.credentials = new networkcredential(login, password);

                // 默认为true,连接不会被关闭

                // 在一个命令之后被执行

                reqftp.keepalive = false;

                // 指定执行什么命令

                reqftp.method = webrequestmethods.ftp.deletefile;

                ftpwebresponse response = (ftpwebresponse)reqftp.getresponse();

                response.close();

                //successinfo

                return string.format("文件{0}已成功删除", fileinf.name);
            }

            catch (exception ex)
            {
                //errorinfo 
                return string.format("文件因{0},无法删除", ex.message);
            }

        }