FTP资源下检测URL地址下文件大小

代码:

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



        }

 

你可能感兴趣的:(文件大小)