Exemplo n.º 1
0
        private static void DisconnectAllLocalNetworkDrives()
        {
            Console.WriteLine();
            foreach (DriveInfo networkDrive in DriveInfo.GetDrives().Where(
                         (drive) => drive.DriveType == DriveType.Network))
            {
                //Shave the "\" and call drop function.  Note:  Remaps on relogin and doesn't force close.
                string networkDriveName = networkDrive.Name.Substring(0, 2);
                //Force disconnect network shares
                int result = InvokeWindowsNetworking.DropNetworkConnection(networkDriveName, 0, true);

                switch (result)
                {
                case InvokeWindowsNetworking.NO_ERROR:     //Success
                    log.Info($"Disconnect drive: {networkDriveName}");
                    break;

                case InvokeWindowsNetworking.ERROR_BAD_PROFILE:
                    log.Error($"Disconnect drive - Bad profile: {networkDriveName}");
                    break;

                case InvokeWindowsNetworking.ERROR_CANNOT_OPEN_PROFILE:
                    log.Error($"Disconnect drive - Cannot open profile: {networkDriveName}");
                    break;

                case InvokeWindowsNetworking.ERROR_DEVICE_IN_USE:
                    log.Error($"Disconnect drive - Device in use: {networkDriveName}");
                    break;

                case InvokeWindowsNetworking.ERROR_EXTENDED_ERROR:
                    log.Error($"Disconnect drive - Extended error: {networkDriveName}");
                    break;

                case InvokeWindowsNetworking.ERROR_NOT_CONNECTED:
                    log.Error($"Disconnect drive - Device not connected: {networkDriveName}");
                    break;

                case InvokeWindowsNetworking.ERROR_OPEN_FILES:
                    log.Error($"Disconnect drive - Error open files: {networkDriveName}");
                    break;

                default:
                    log.Error($"Disconnect drive - Unknown error code: {networkDriveName}");
                    break;
                }
            }
            Console.WriteLine();
        }
Exemplo n.º 2
0
        private static bool ConnectNetworkDrives(HikariModel model)
        {
            log.Info("Connecting network drives...");
            Console.WriteLine();
            bool   success     = true;
            Forker driveForker = new Forker();

            foreach (var drive in model.Drives)
            {
                Tuple <string, string> exp_unc = (drive.Value).FirstOrDefault();
                string driveLetter             = drive.Key;
                string expression = exp_unc.Item1;
                string unc        = exp_unc.Item2;
                driveForker.Fork(delegate
                {
                    if (!Directory.Exists(unc))
                    {
                        log.Error($"Network path \"{unc}\" doesn't exist! Drive {driveLetter} for [{expression}] is NOT connected!");
                        success = false;
                    }
                    else
                    {
                        string result = InvokeWindowsNetworking.connectToRemote(driveLetter, unc);
                        if (!string.IsNullOrEmpty(result))
                        {
                            log.Error($"Network drive {driveLetter} -> \"{unc}\" ERROR: {result}");
                            success = false;
                        }
                        else
                        {
                            log.Info($"{driveLetter} -> \"{unc}\" [{expression}]");
                        }
                    }
                });
            }

            driveForker.Join();

            return(success);
        }