How to determine if a network path is available or

2019-04-07 07:32发布

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!

3条回答
仙女界的扛把子
2楼-- · 2019-04-07 07:36

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

bool folderExists = Directory.Exists(@"\\Path\To\Folder");
查看更多
祖国的老花朵
3楼-- · 2019-04-07 07:41

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.

查看更多
放荡不羁爱自由
4楼-- · 2019-04-07 07:59

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?

查看更多
登录 后发表回答