2

I am using third party FTP server to upload files there. But before uploading, I want to implement test connection functionality, so user can test that he has entered correct credentials or not.

The problem is that if user don't have access to list the directory. So my code will not work for testing connection even he has access for file upload, because right now I am using WebRequestMethods.Ftp.ListDirectory method to test connection and its working as expected.

bool testResult = false;

try
{
    FtpWebRequest request =
        (FtpWebRequest)WebRequest.Create(objVMTestFTPConnectionRequest.FTPAddress);

    request.Method = WebRequestMethods.Ftp.ListDirectory;

    request.Credentials =
        new NetworkCredential(
            objVMTestFTPConnectionRequest.UserName, objVMTestFTPConnectionRequest.Password);

    WebResponse response = request.GetResponse();

    testResult = true;
}
catch (Exception exception)
{               
    throw;
}
return testResult;

I am getting following error when I remove that ListDirectory method

The requested URI is invalid for this FTP command.

Just help me if I can do this without using that method.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Athar Sayed
  • 43
  • 1
  • 14

1 Answers1

1

Use WebRequestMethods.Ftp.PrintWorkingDirectory:

  • It is noop;
  • It has low impact;
  • And should work on any server.

To allow testing credentials, you also need to disable FtpWebRequest.KeepAlive. See:
FtpWebRequest ignores wrong password after previous successful connection using correct password

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992