+1 vote
by (130 points)
edited

Hi Can I get the FTP root directory when i working in Sub directories of FTP or how can i store the ftp root directory value constantly as I need to use it in the middle of the program.

Please prefer vb.net

My Heart full thanks to Replies

Applies to: Rebex FTP/SSL

4 Answers

0 votes
by (58.9k points)
edited

Most FTP servers use remote filesystem with a single root accesible at "/". So you could use ChangeDirectory("/") to switch into FTP server's root directory like this:

' create client, connect and log in
Dim client As New Ftp
client.Connect("ftp.example.org")
client.Login("username", "password")

' change the current directory to root
client.ChangeDirectory("/")

client.Disconnect()
by (900 points)
edited

Any other way to get root directory.?

by (58.9k points)
edited

Could you please share with us what is the thing you want to achieve? And why is changing into the root with the ChangeDirectory("/") command not suitable for you?

by (58.9k points)
edited

@ganeshspy I have come up with another method to detect the root directory. See this answer.

0 votes
by (160 points)
edited

Hello Tomas

Thanks for your reply. If the root directory is "/" then we can use client.ChangeDirectory("/") scenario. But all the ftp root directories must not be "/", in such case what we have to do?

Here is my actual problem...

I am dynamically creating the download path, that I need to concatenate with the root directory to download. This step will be continued until some condition. But when I changed the current path to path that I created for download the files, it is not working for next step......

Would you help me please?

Thanks

by (58.9k points)
edited

In fact almost any normal FTP server running on Unix or Windows would have the "/" as a root. See this answer for more details.

+2 votes
by (58.9k points)
edited

If you need to programmatically get the home directory name, you could use GetCurrentDirectory method like this: (provided root is the current directory after you login.)

        Ftp ftp = new Ftp();
        ftp.Connect("server");
        ftp.Login("user","password");

        string homeDir = ftp.GetCurrentDirectory();

        // do work

        ftp.Disconnect();
by (160 points)
edited

I got it.

Thanks Tomas,

by (900 points)
edited

Any other way to get root directory in same connection ?

by (58.9k points)
edited

Sorry the code above actually detects the home direktory, not the root direktory. However, detecting the home dir helped the user. I have edited my answer to correct the mistake.

+1 vote
by (58.9k points)
edited

Unfortunatelly, there is no firm standard stating how the root directory of an FTP server should be named, nor even whether there has to be a root directory.

In fact almost any normal FTP server running on Unix or Windows would have the "/" as a root.

Theoretically, there might be FTP servers with a strange root directory name other than "/" that still use the concept of directory (so you can go out of the directory with the ftp.ChangeDirectory("..") or some other command which you have to know). For these servers I have come up with an algorithm to detect root. Basically it tries to jump out of the current directory and if the directory after the jump is the same, then it is the root. The maximum number of attempts is applied to make sure the algorithm ends even for the weirdest FTP servers.

Here is the DetectFtpRoot method, it will work also for normal FTP servers with the most commion "/" root dir:

    string DetectFtpRoot(Ftp ftp)
    {
        string up = "..", oldDir, currentDir;
        int maxTries = 100;
        int i = 0;
        string root = null;

        oldDir = ftp.GetCurrentDirectory();

        while (i < maxTries)
        {
            ftp.ChangeDirectory(up);
            currentDir = ftp.GetCurrentDirectory();
            i++;

            if (oldDir == currentDir)
            {
                root = oldDir;
                break;
            }
            oldDir = currentDir;
        }

        if (root == null)
            throw new Exception("Unable to detect FTP server root, maximum retries number reached.");

        return root;
    }

Please note that there are also some weird FTP servers out there that for instance even do not use the concept of "directories" so in this case I think there is no point talking about any concept of 'root' directory here at all. For these FTP servers the algorithm above will not work for obvious reason - the ChangeDirectory("..") method will have no effect. The above algorithm also will not work, if you do not know how the FTP server names the parent directory (".."). We do not know about any means to detect the root directory in these cases. This also applies to servers which do not support the ChangeDirectory("..") method.

...