Solved

Get Connection Status

Posted on 2014-11-26
7
64 Views
Last Modified: 2014-11-27
How do you check connection status using .Net Framework 4.5?

Here's what I have:

Imports System.Net

Public Class FTPPlus2
   Shared Property HostName As String
   Shared Property Password As String
   Shared Property UserID As String
   Shared Property Target As String
   Shared Property ID As String
   Shared Property SiteName As String

   Shared Property request As FtpWebRequest
   Shared Property response As FtpWebResponse

   Shared Sub Disconnect()
      If response IsNot Nothing Then
         response.Close()
      End If
   End Sub

   Shared Sub Connect()
      request = DirectCast(WebRequest.Create(String.Format("ftp://{0}", HostName)), FtpWebRequest)
   End Sub

   Shared Sub ChangeCurrentFolder(FolderName)

   End Sub

   Shared Function Connected As Boolean
      Dim ret As Boolean  = False

      Return ret
   End Function

   Shared Sub Login()
      request.Credentials = New NetworkCredential(UserID, Password)
   End Sub
End Class

Open in new window

0
Comment
Question by:Ray Turner
7 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40466990
If con.State = con.Open Then
	Connected  = true
Else
	Connected  = false
End If

Open in new window

0
 
LVL 1

Expert Comment

by:cstaff16
ID: 40467072
Are you trying to determine if you can connect to the internet?
If so, you can do something like this.  I've used this in various apps and it works well.
You can use any url you want, but I just use google.

using the System.Net.WebClient

public static bool CheckForInternetConnection()
{
    try
    {
        using (var client = new WebClient())
        using (var stream = client.OpenRead("http://www.google.com"))
        {
            return true;
        }
    }
    catch
    {
        return false;
    }
}
0
 
LVL 1

Expert Comment

by:cstaff16
ID: 40467078
Sorry, here is vb.net version :).

Public Shared Function CheckForInternetConnection() As Boolean
      Try
            Using client = New WebClient()
                  Using stream = client.OpenRead("http://www.google.com")
                        Return True
                  End Using
            End Using
      Catch
            Return False
      End Try
End Function
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:Ray Turner
ID: 40467291
I'm trying to determine the status of the FtpWebRequest.  Is there a way to determine if the connection is open/successful?
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40467496
Catch the WebExceptions that GetRequestStream and GetResponse could throw.
0
 

Author Closing Comment

by:Ray Turner
ID: 40469955
Thanks!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now