Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - FTP check/test server

Tags:

c#

ftp

I want to test a given IP and login detail to see whether the FTP server is active/details are correct. Nothing more.

I have looked on the web for a solution but none seem simple/eloquent as, for example, using the Ping class to ping and check for a server. I have the following, which doesn't work:

    static private void testConntecion()
    {
        FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://172.29.200.158/"));
        requestDir.Credentials = new NetworkCredential("dvm_user", "dvm");

        try
        {
            FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse();
            Console.WriteLine("Cool Beans");
        }

        catch(Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

I have also tried:

FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create("ftp://172.29.200.158/");

Error message: System.Net.WebException: The requested URI is invalid for this FTP command. at System.Net.FtpWebRequest.GetResponse()

Thanks in advance.

like image 745
nf313743 Avatar asked Jan 21 '23 04:01

nf313743


1 Answers

You need to set the Method on the ftp request:

requestDir.Method = WebRequestMethods.Ftp.ListDirectory;
like image 78
kͩeͣmͮpͥ ͩ Avatar answered Jan 30 '23 07:01

kͩeͣmͮpͥ ͩ