views:

59

answers:

3

Using .NET / C#, how to determine if a network path (e.g. \mymachine\myfolder) is available or not (online or offline)? Is there a way to be notified by WMI of such event?

Thanks!

A: 

Maybe try the Ping class:

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

It will tell you if a host is available, but I don't know if it will tell you whether a particular share/path is available.

NeilMonday
+2  A: 

You can use Directory.Exists to check if a path exists.

bool folderExists = Directory.Exists(@"\\Path\To\Folder");
Kyle Trauberman
A: 

Just try to use it. It will cause an error condition if it isn't. You have to code against that condition anyway: why do it twice?

EJP
This is costly. I would prefer the OS to notify me.
Martin
That doesn't mean anything. It's more costly to test the same condition twice, and to code the same recovery twice. I'm assuming you're going to use the resource some time. Pre-testing its availability is essentially invalid: you are introducing a timing window during which it can become unavailable after you tested it. Basically you are asking the computer to predict the future.
EJP