开发者

ftp .net getdirectory size

hi i write method which must to know that is size of specified directory i get response from server which contains flags of file name size and other info and on the different ftp servers format of answer is different how to know format of answer?

unsigned long long  GetFtpDirSize(String^ ftpDir) {
unsigned long long  size = 0;
int j = 0;
StringBuilder^ result = gcnew StringBuilder();
StreamReader^ reader;
FtpWebRequest^ reqFTP;
reqFTP = (FtpWebRequest^)FtpWebRequest::Create(gcnew Uri(ftpDir));
reqFTP->UseBinary = true;
reqFTP->Crede开发者_JAVA技巧ntials = gcnew NetworkCredential("anonymous", "123");
reqFTP->Method = WebRequestMethods::Ftp::ListDirectoryDetails;
reqFTP->KeepAlive = false;
reqFTP->UsePassive = false;
try {
    WebResponse^ resp = reqFTP->GetResponse();
    Encoding^ code;
    code = Encoding::GetEncoding(1251);
    reader = gcnew StreamReader(resp->GetResponseStream(), code);
    String^ line = reader->ReadToEnd();
    array<Char>^delimiters = gcnew array<Char>{
        '\r', '\n'
    };
    array<Char>^delimiters2 = gcnew array<Char>{
        ' '
    };
    array<String^>^words = line->Split(delimiters, StringSplitOptions::RemoveEmptyEntries);
    array<String^>^DetPr;
    System::Collections::IEnumerator^ myEnum = words->GetEnumerator();
    while ( myEnum->MoveNext() ) {
        String^ word = safe_cast<String^>(myEnum->Current);
        DetPr = word->Split(delimiters2);
   }
}


Basically, you can't. You are interpreting the raw result and there is no defined format for this data (or is there any requirement that this data be returned at all in the response). And the FTP protocol does not define any other way of getting this.

What that leaves you with is a collection of parsing patterns for the server types you know about and working through them looking for valid data. Not entirely easy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜