Solved

Get Connection Status

Posted on 2014-11-26
7
71 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

815 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

9 Experts available now in Live!

Get 1:1 Help Now