Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get Connection Status

Posted on 2014-11-26
7
Medium Priority
?
78 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
Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

 

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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

722 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