Solved

Get Connection Status

Posted on 2014-11-26
7
77 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 34

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 75

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

636 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