Networking - How to check if file server is available?

Asked By Nurko on 14-Nov-11 05:00 PM
I would like to check if a remote file server is available from a C# application.
Currently I check DirectoryInfo.Exists property but for that I need to know a name of at least one shared folder on that remote server. This is not what I want. I would like to check if there is any folder shared on a remote server. Something that is perfomed on Windows when I type \\servername on windows explorer address bar. If the server has shared folders I get the list of them, if not I get an error message. I guess that some share enumerator functions exist in windows api but don't know where to find them. If .net has such function it would be perfect, if not p/invoking is ok.
Riley K replied to Nurko on 14-Nov-11 08:13 PM

To retrieve the UNC path for a file on a mapped drive, callWNetGetUniversalName.

If the path is on a local shared folder, WNetGetUniversalName won't help. In this case, you need to call NetShareEnum and look for a matching path. 

Refer this link

Kirtan Patel replied to Nurko on 14-Nov-11 09:59 PM
Step 1 : Add Reference to System.Management
Step 2 : Write Code like below
using (ManagementClass shares = new ManagementClass(@"\\NameOfTheRemoteComputer\root\cimv2", "Win32_Share", new ObjectGetOptions()))
  foreach (ManagementObject share in shares.GetInstances())
Jitendra Faye replied to Nurko on 14-Nov-11 10:51 PM

After Pinging you can know the status og Remote server existance.

Use this code-

using System;
using System.Management;

namespace FX.Network.Utility
    public class NetworkDiscovery
        public NetworkDiscovery() { }

        public static bool Ping(string Target)
            SelectQuery query = new SelectQuery("Win32_PingStatus", string.Format("Address='{0}'", Target));
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);

            foreach (ManagementObject result in searcher.Get())
                return (result["StatusCode"] != null && (0 == (UInt32)result["StatusCode"]));
            return false;

Note- If return value is true means server is present

TRy this and let me know,.
Nurko replied to Jitendra Faye on 24-Nov-11 10:01 AM
Thanks for your help, but this is not what I want.
I don't want to ping remote host (I know how to do it without WMI), but to check if file sharing is working.

Nurko replied to Kirtan Patel on 24-Nov-11 10:02 AM
Thanks for help. I get access denied exception.
Do I need admin access on remote computer to run the code?
Nurko replied to Riley K on 24-Nov-11 12:00 PM
Thank you Riley K for help.
I found NetShareEnum quite useful but it has a feature that makes me trouble - offline files. NetShareEnum returns me names of shared folders even if the file server is not connected. It happens because some folders on the server are available offline. Do you have an idea how to overcome this?