代码:

string ftpServerIP;
string ftpUserID;
string ftpPassword;
FtpWebRequest reqFTP;

//获得文件大小
        public long GetFileSize(string filename)
        {
            long fileSize = 0;
            try
            {
                FileInfo fileInf = new FileInfo(filename);
                string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
                Connect(uri);//连接     
                reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                fileSize = response.ContentLength;
                response.Close();
            }
            catch (Exception ex)
            {
                if (OnErrorEvent != null) OnErrorEvent(ex.Message);
            }
            return fileSize;
        }

其中Connect(uri)

    private void Connect(String path)//连接ftp
        {
            // 根据uri创建FtpWebRequest对象
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));

            // 指定数据传输类型
             reqFTP.UseBinary = true;
             //reqFTP.UsePassive = false;

            // ftp用户名和密码
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

        }

 

相关文章:

  • 2021-09-06
  • 2021-10-04
  • 2021-06-10
  • 2021-08-18
  • 2022-12-23
  • 2022-03-05
  • 2022-12-23
  • 2021-06-22
猜你喜欢
  • 2022-02-08
  • 2021-08-06
  • 2022-02-14
  • 2021-07-02
  • 2022-01-25
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案