3

If I want to check server availability but I don't know about its shares, I can type its UNC name (or IP address) into Windows Explorer window even without server share directory:

Explorer screenshot from Windows 8

How I can use this programatically in .NET to verify whether server is online? I'm trying

My.Computer.FileSystem.DirectoryExists(UNCPath)

but this works only for UNC paths like \\MY_SERVER\Users, it is not inteded to use with \\MY_SERVER.

I would like to avoid Ping and stick with functionality which works when file system works because some servers can have ICMP_ECHO disabled due to security reasons.

(In case if you wish to post some code, feel free to choose VB or C#.)

nvoigt
  • 75,013
  • 26
  • 93
  • 142
miroxlav
  • 11,796
  • 5
  • 58
  • 99
  • What is `My.Computer.FileSystem...`? – musefan Feb 21 '14 at 11:32
  • I think the problem is because `"\\MY_SERVER"` is NOT a directory, it is a server name. `"\\MY_SERVER\Users"` is a directory, and it is the root directory in terms of what has been shared so there is no parent to that for you to check. – musefan Feb 21 '14 at 11:37
  • 1
    @musefan `My.Computer.FileSystem` is a [VB-exclusive convenience for functionalities from System.IO.File](http://stackoverflow.com/questions/11778319/what-is-the-equivalent-of-my-computer-in-c-sharp) – miroxlav Feb 21 '14 at 11:46
  • Don't none of the answers is the duplicate question I linked work for you? – musefan Feb 21 '14 at 12:07
  • @musefan Suggested solution gave the hint to use WMI to check availability of remote service which facilitates file and sharing services. This should reveal availability of the server. I have no experience with WMI so I'm not sure if that can be done without prior authentication at remote computer. Thus I'm leaving this question open, maybe someone can still add a good answer here. – miroxlav Feb 21 '14 at 12:58
  • The [second answer](http://stackoverflow.com/a/1663287/266143) is exactly your approach. – CodeCaster Feb 22 '14 at 01:00
  • Opening a socket isn't really rocket science that need to be explicitly shown. I don't know anymore what's most common, feel free to leave the question open. The community will clean it up if it seems necessary. – CodeCaster Feb 22 '14 at 09:41

1 Answers1

3

Edit: I'm notified this may be duplicate solution to Q/A mentioned elsewhere. The difference is that the other question accepts the answer which did not really work for me and I have reasons to think that using WMI is limiting approach in this scenario (it requires credentials, see below). In this answer, I'm giving an approach which really works for me. But if you really think it is duplicate, continue marking it for closing and I'll remove the entire question as unnecessary.


I've continued studying and trying and finally I settled with using TCP port 139, the same which is used by file sharing mechanism. Consider the following source as a draft (and do not forget to handle exceptions etc.), but finally I can have piece of code which seems to work for me:

Public Shared Function IsFileShareServerOnline(PCName As String,
                                               Timeout as Integer) As Boolean

    Const Port As Integer = 139 'port for file sharing service

    Dim IsOnline As Boolean = False
    Dim IP As Net.IPAddress = Net.Dns.GetHostAddresses(PCName)(0)
    Dim Address As Net.IPAddress = Net.IPAddress.Parse(IP.ToString())
    Dim Socket As Net.Sockets.TcpClient = New Net.Sockets.TcpClient(Address.AddressFamily)
    Dim Connect = Socket.BeginConnect(Address, Port, Nothing, Nothing)
    Dim WaitingResult = Connect.AsyncWaitHandle.WaitOne(Timeout, False)

    If (Connect.IsCompleted) Then
        If WaitingResult Then
            Socket.EndConnect(Connect)
            IsOnline = True
        End If
    End If
    Socket.Close()
    Return IsOnline

End Function

The code has an issue that on first run it cannot detect remote computer even if available. Upon retry it works. The problem renews (one bad detection) whenever the connection is attempted after longer time. I'm not TCP/IP expert to understand the cause, maybe I use too short timeout (1 sec). A workaround is to give at least 2 connection attempts.

Before trying this approach, I've also implemented and tested WMI-based queries, but they did not work without prior authentication at remote computer what was pretty useless in my scenario.

Community
  • 1
  • 1
miroxlav
  • 11,796
  • 5
  • 58
  • 99