1

I´m trying to open an FTP connection in which I cannot execute more elaborated commands, such as LIST or file handling commands.

All I need to know is if the connection succeeds and my credentials are accepted by the server. This is a small command line utility to monitor the connectivity of an industrial machine with our network.

If the utility fails to connect, then is likely that the FTP daemon in our server is down and I´ll make the utility to send an e-mail to our network guys so they can restablish the daemon before the machine needs to query files, what occurs at every 30 minutes average.

The error I´m getting is that when I execute the line dirFtp.Method = "STAT" I get an error "This method is not supported. Parameter name: value"}"

I know that my command is not being digested by the framework, however I don´t know how I can pass it to the FTP server, to then get the server response.

I looked into the class WebRequestMethods.Ftp Class to pass the commands to the server but all commands supported by this class somehow attempt to list the folder contents or handle files, what fails for the purpose I need. I need to be able to pass a simple command like STATS or NOOP and get the server response. That´s enough for me to determine if the server is available or not.

That being said, how can I pass a FTP STATS or NOOP command to the server in .net?

Imports System
Imports System.Net
Imports System.IO
Imports System.Text

Module Main

    Sub Main()
        ConectarFTP("FTPServerName", "JohnDoe", "Pass123")
    End Sub

        Sub ConectarFTP(ByVal Servidor As String, ByVal Usuario As String, ByVal Senha As String)

        Dim dirFtp As FtpWebRequest = CType(FtpWebRequest.Create("ftp:\\" & Servidor), FtpWebRequest)

        ' Credenciais
        Dim cr As New NetworkCredential(Usuario, Senha)
        dirFtp.Credentials = cr

        ' Comando a executar
        dirFtp.UsePassive = True
        dirFtp.KeepAlive = False
        dirFtp.Method = "STAT"

        'dirFtp.Method =  WebRequestMethods.Ftp .

        Dim reader As New StreamReader(dirFtp.GetResponse.GetResponseStream)

        ' Ler o stream
        Dim res As String = reader.ReadToEnd()

        ' Mostrar.
        Console.WriteLine(res)

        ' Fechar o stream aberto.
        reader.Close()
    End Sub

End Module
Daniel Santos
  • 188
  • 2
  • 15
  • The question seems to be solely about vb.net - no c# involved - why the addition of a c# tag? – David Wilson Feb 15 '16 at 19:38
  • Well, it's a .net question... The code above is VB but the solution in C# will also answer this because the method or function I have to use works in both languages.... – Daniel Santos Feb 15 '16 at 20:38
  • Also, VB and FTP is not a usual combination. Making it a .net question increases the possibility for a response. – Daniel Santos Feb 15 '16 at 21:24

0 Answers0