MSDN Forums Discussion
Retrive the Network Drives, iterate them and call the drop method on each.Code:/// <summary> /// API Call to Drop existing Network Connection /// </summary> /// <param name="lpName">Pointer to a constant null-terminated string that specifies the name of either the redirected local device or the remote network resource to disconnect from.</param> /// <param name="dwFlags">Connection type. 0 to retain only for current session or CONNECT_UPDATE_PROFILE to persist on session being logged out.</param> /// <param name="fForce">Specifies whether the disconnection should occur if there are open files or jobs on the connection. If this parameter is FALSE, the function fails if there are open files or jobs.</param> /// <returns></returns> [System.Runtime.InteropServices.DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2A", CharSet = System.Runtime.InteropServices.CharSet.Ansi)] private static extern int DropNetworkConnection(string lpName, int dwFlags, bool fForce); internal const int NO_ERROR = 0x000; internal const int ERROR_BAD_PROFILE = 0x4B6; internal const int ERROR_CANNOT_OPEN_PROFILE = 0x4B5; internal const int ERROR_DEVICE_IN_USE = 0x964; internal const int ERROR_EXTENDED_ERROR = 0x4B8; internal const int ERROR_NOT_CONNECTED = 0x8CA; internal const int ERROR_OPEN_FILES = 0x961;
Code://Iterate on the IEnumerable<DrifeInfo> returned from the Lambda Expression foreach (System.IO.DriveInfo networkDrive in System.IO.DriveInfo.GetDrives().Where( (drive) => drive.DriveType == System.IO.DriveType.Network)) //Shave the "\" and call drop function. Note: Remaps on relogin and doesn't force closure. switch (DropNetworkConnection(networkDrive.Name.Substring(0, 2), 0, false)) { case NO_ERROR: //Success break; case ERROR_BAD_PROFILE: //TODO: Handle break; case ERROR_CANNOT_OPEN_PROFILE: //TODO: Handle break; case ERROR_DEVICE_IN_USE: //TODO: Handle break; case ERROR_EXTENDED_ERROR: //TODO: Handle break; case ERROR_NOT_CONNECTED: //TODO: Handle break; case ERROR_OPEN_FILES: //TODO: Handle break; default: //TODO: Handle Unsupported Error Code break; }


Reply With Quote