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)
currentDir = ftp.GetCurrentDirectory();
if (oldDir == currentDir)
root = oldDir;
oldDir = currentDir;
if (root == null)
throw new Exception("Unable to detect FTP server root, maximum retries number reached.");
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.