zoukankan      html  css  js  c++  java
  • C#相关FTP操作

    C# 相关 FTP(File Transfer Protocol) 操作

    1.获取文件目录

    UsePassive:需要设置为false。 使用被动模式,因为客户端防火墙的存在,所以为了确保数据可以正常传输,所有采用被动模式。

    UseBinary:指示文件的传输类型,true 指示二进制数据,false指示数据为为本,默认为true。

        /// <summary>  
        /// 根据ftp路径返回目录下的文件名称
        /// </summary>  
        public string[] GetFilesDetailList(string path)
        {	
            List<string> listFileNames = new List<string>();
            FtpWebRequest ftp;
            ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
            ftp.UsePassive = false;
            //ftp.UseBinary = true;
            ftp.Credentials = new NetworkCredential("Skills", "Skills"); //用户名密码
            ftp.Method = WebRequestMethods.Ftp.ListDirectory; //只带文件名
    
            var stream = ftp.GetResponse().GetResponseStream();
    
            StreamReader reader = new StreamReader(stream);
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                listFileNames.Add(line);
            }
            reader.Close();
            return listFileNames.ToArray();
        }
    

    2.下载文件

        /// <summary>  
        /// 下载文件
        /// </summary>  
        public void DownloadFile(string filePath)
        {
            FtpWebRequest ftp;
            ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(filePath));
            ftp.UsePassive = false;
            ftp.UseBinary = true;
            ftp.Credentials = new NetworkCredential("Skills", "Skills"); //用户名密码
            ftp.Method = WebRequestMethods.Ftp.DownloadFile;
            Stream stream = ftp.GetResponse().GetResponseStream();
    
            //Save file
            string fileName = Path.GetFileName(filePath);
            FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate);
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int readCount = stream.Read(buffer, 0, bufferSize);
            while (readCount > 0)
            {
                fs.Write(buffer, 0, readCount);
                readCount = stream.Read(buffer, 0, bufferSize);
            }
            fs.Close();
            stream.Close();
        }
    

    3.获取文件大小

            /// <summary>  
            /// 获取指定文件大小  
            /// </summary>  
            public long GetFileSize(string filename)
            {
                FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(filename));
                reqFTP.Method = WebRequestMethods.Ftp.GetFileSize; //获取文件大小
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential("Skills", "Skills");
                return reqFTP.GetResponse().ContentLength;
            }
    

    学习自:https://www.cnblogs.com/rushme/p/6690912.html

  • 相关阅读:
    JS URL传中文参数引发的乱码问题
    git (转载)
    php webservice
    php date之间的相互转换
    在图片上加字符-base64转图片-图片转base64
    转:微软面试一百题
    2014-8-17 note
    some base knowledge
    introduction
    Centos6.6 编译安装nginx
  • 原文地址:https://www.cnblogs.com/-Tiger/p/8682592.html
Copyright © 2011-2022 走看看