As far as I know, there are still some low level tasks that .NET cannot do, but in this instance I know it can be done, because I’ve seen Microsoft Office and Windows File Explorer waking up a sleeping drive. I know that a lot of things can cause the red X to appear on a drive in Windows File Explorer, what is not evident or exposed to .NET, is how Microsoft apps some how ‘wakes up’ the sleeping drive.
And this is not those cases where you don’t have permission, or the server path has changed etc., this is the ongoing issue, where it worked fine a few hours ago, and now it’s disconnected. Not only that, printing is affected and so is access to a SQL Server instance. Ie: the NIC itself has gone to sleep, because of some power saving setting. (And yes I know, one could disable all the green settings, but what’s the point of having them if they don’t work well with non MS applications.)
This issue has been around for a very long time and so far there is no resolution.
The closest I can get is this Windows API call: WNetRestoreConnectionW (now depreciated) and System.Net.NetworkInterface.OperationalStatus == 5 (Dormant), assuming I have the right network interface for the mapped drive. Or …NetworkInterface.GetIsNetworkAvailable(), or DriveInfo.IsReady. Or an exception when using sleepy network drive.
So there are plenty of ways to determine if a Network, and whether it’s available or not, but there is nothing like this (yet):
var sDrive = new DriveInfo(“s”);
if (!sDrive.DriveType == DriveType.Network && sDrive.IsReady())
var networkInterface == sDrive.GetNetworkInterface();
if (!networkInterface.GetIsNetworkAvailable() || networkInterface.OperationalStatus == OperationalStatus.Dormant)
Someone at Microsoft has developed a fix, but it hasn’t made it to .NET yet.