List files on ftp server

Hello all,

i have created an ftp client in C# using Wininet and i am able to download upload files from and to my ftp server, what i would like is to have the list of files and directories on the ftp server using wininet.

any help would be great.




Answer this question

List files on ftp server

  • IoDavide

    Anyone has any idea

  • Marcus the Maker

    Pierre,

    I haven't used wininet on .NET (and I don't plan to do so for the foreseeable future), yet there are a couple of things that might help.

    - You should check the result of FtpFindFirstFile. If I understood the docs, it should be zero (or IntPtr.Zero) if the file does not exist.

    - Before you call InternetFindNextFile, you should look at what you got with the call to FindFirstFIle... in your case you are skipping the first file returned (if any).

    - When calling InternetFindNextFile, you should check the return that will tell you if there are more files or not.

    In a nutshell:

    IntPtr pEnum = FtpFindFirstFile (hConnect, ...);
    if (pEnum != IntPtr.Zero) {
    do {
    Console.WriteLine (lpFindData.fileName); // do whatever you want
    } while (InternetFindNextFile (pEnum, ref lpFindData));
    }

    I suspect that the truncated name you get is the result of the API putting zero's to "clear" the string the C way, which stays printable by C#.
    I bet the API is trying to tell you "no more files" through the return value, this would explain why the returned filename is not changing.

    Also, as far as I remember you need a pattern and, maybe, a full path. Try using: "/some/path/*.*"

    HTH
    --mc


  • Brad Albrecht

    I don't know if I can answer your question, but it's impossible to know without seeing your code.

  • didalos

    With WinInet you'll have to PInvoke FtpFindFirstFile and InternetFindNextFile.

    Just FYI: there is an FtpWebRequest class in 2.0 that can make some operations easier, but doesn't actually provide as detailed an API as WinInet.



  • wayneg

    any idea guys

  • Grant Carthew

    Hello argt,

    here's the part of the code i am talking of

    hInternet hConnect pEnum are IntPtr, lpFindData is a FileData class

    hInternet = ftp.Open("", accessType, sProxy, null, uOpenFlag);

    hConnect = ftp.Connect(hInternet, serverBox.Text, 21, usernameBox.Text, passwordBox.Text, uService, uConnectFlag,0);

    if (hConnect != IntPtr.Zero)

    {

    statusBox.Text = "Connected!";

    pEnum = ftp.FindFirstFile(hConnect, "", lpFindData, 0, 0);

    ftp.FindNextFile(pEnum, ref lpFindData);

    if (lpFindData.fileName != null)

    {

    fileBox.Items.Add(lpFindData.fileName + " " + lpFindData.fileAttributes);

    ftp.FindNextFile(pEnum, ref lpFindData);

    fileBox.Items.Add(lpFindData.fileName);

    ftp.FindNextFile(pEnum, ref lpFindData);

    fileBox.Items.Add(lpFindData.fileName);

    }

    ftp.GetLastResponseInfo(out errorCode, buffer, ref bufferLength);

    MessageBox.Show(buffer, "Login Status: "+errorCode.ToString());

    for the 3 calls of FindNextFile i get the same filename as findfirstfile, microsoft says that to start an enumaration from the begining i should use either null or "" as the 2nd parameter of FindFirstFile.When i use "Pierre Charbel.txt" as 2nd parameter in this method i get erre Charbel.txt as file name



  • kavita1000

    Hello Mario thanks for answering and making researches abt wininet, but i'd like to comment what you said first FindFirstFile is returning a non null pointer and is working well except for the returning filename which is truncated, then i have an FTP server at home and there r about 5 or 6 files and several folders on it for test purpose but i am not getting the other files or folders, and the docs you read say that if the filename of FindFirstFile is set to null or "" then this functions will start the enumeration of the files and folders on the ftp server and a loop of InternetFindNextFile will enumerate the rest of the files.

  • ebroms

    i want the listing of the ftp server using wininet

  • Charles Willis

    Why don't you use a full managed Ftp Library

    There is a free FTP component writen by Jerome Lacaille and i availeble trough the code project. You can read the article and download the component here.

    Here is a little extra list of components:



  • soniaarora

    hi Scott,

    thanks for answering, i know i should use FtpFindFirstFile to start a listing and then InternetFindNextFile but when i do so, (lets say i call 3 times InternetFindNextFile), i get 3 times the same file name but truncated by 2 letters always. ex : on my ftp server i have the files abc.txt def.txt and ftp.txt when i call the function twice i get c.txt c.txt.

    how can i list the complete files and directories on my ftp server

     any idea would be more than the very welcome



  • List files on ftp server