static void ConnectTest(NetworkDrive drive) { Console.WriteLine("Press any key to connect..."); Console.ReadKey(); Console.WriteLine("Connecting..."); drive.Connect(); Console.WriteLine("Connected: {0}", drive.IsConnected); }
private void RenewDrivePassword(NetworkDrive drive) { renewDriveMutex.WaitOne(); try { bool keepAsking = true; do { App.Current.Dispatcher.Invoke(new Action(() => { CredentialsWindow dialog = new CredentialsWindow(); dialog.Description = string.Format(Properties.Strings.CredentialsWindowReEnterAfterFailDescription, Helper.GetDomain(drive.RemoteAddress), AskForCredentialsAfterFails); dialog.Username = drive.Username; if (dialog.ShowDialog() == true) { drive.SetCredentials(dialog.Username, dialog.Password); } else { keepAsking = false; } })); if (keepAsking) { if (CheckNetworkDrive(drive)) { try { drive.Connect(ConnectDriveFlags.IgnoreAlreadyConnected | ConnectDriveFlags.Force); } catch (Exception ex) { this.log.Warn("Could not connect drive \"" + drive.LocalDriveLetter + "\" after password renewal: " + ex.Message, ex); App.Current.Dispatcher.Invoke(new Action(() => { MessageBox.Show(null, string.Format(Properties.Strings.MessageConnectFailedAfterCheck), Properties.Strings.TitleCheckConnection, MessageBoxButton.OK, MessageBoxImage.Exclamation); })); } keepAsking = false; } } } while (keepAsking); App.Current.DriveCollection.SaveDrives(); } catch (Exception ex) { this.log.Info("Could not renew password for drive \"" + drive.LocalDriveLetter + "\": " + ex.Message, ex); } renewDriveMutex.ReleaseMutex(); }
internal static void CanBeConnected(NetworkDrive drive) { try { drive.Connect(); ConnectionAsserts.Assert_Connected(drive); } catch (OperationCanceledException) { ConnectionAsserts.Assert_Disconnected(drive); } }
private void CheckDriveConnectivity(NetworkDrive drive) { if (drive.TargetConnectivity) { if (!drive.IsConnected) { this.log.Info("Drive \"" + drive.LocalDriveLetter + "\" lost it's connection to \"" + drive.RemoteAddress + "\" -> Restoring..."); try { drive.Connect(ConnectDriveFlags.IgnoreAlreadyConnected | ConnectDriveFlags.Force | ConnectDriveFlags.DoNotUpdateTargetState | ConnectDriveFlags.NoTimeout); } catch (WNetApiException ex) { this.log.Error("Windows-API returned " + ex.ErrorCode + " for drive \"" + drive.LocalDriveLetter + "\": " + ex.Message); } } } }
void ConnectDrive(NetworkDrive drive) { int startTime = Environment.TickCount; log.Info("Background connection task for drive \"" + drive.LocalDriveLetter + "\" to \"" + drive.ExpandedRemoteAddress + "\" started"); int tries = 0; int credentialErrors = 0; int lastErrorCode = -1; do { // wait some time before connecting again if (tries > 0) { int sleepTime; lock (this.rnd) sleepTime = this.rnd.Next(ConnectionRetryIntervalMin, ConnectionRetryIntervalMax); Thread.Sleep(sleepTime); } if (!this.drives.ContainsNetworkDrive(drive)) { this.log.Info("Background task for drive \"" + drive.LocalDriveLetter + "\" stopped: Drive was remove from the list"); return; } if (drive.DoNotChangeConnectionState) { // would have been tried, so count it tries++; continue; } try { if (drive.ConnectOnStartup && drive.TargetConnectivity) { if (!App.Current.ApplicationOptions.ForceReconnectAtStartup && drive.IsConnected) { this.log.Info("Background task for drive \"" + drive.LocalDriveLetter + "\" stopped: Drive is already connected."); return; } try { tries++; if ((tries % PrintStatusInterval) == 0) { // regular status messages in the log file this.log.Info("Try " + tries + " for drive \"" + drive.LocalDriveLetter + "\": IsConnected=" + drive.IsConnectedCached + "; LastErrorCode=" + lastErrorCode); } drive.Connect(ConnectDriveFlags.IgnoreAlreadyConnected | ConnectDriveFlags.Force | ConnectDriveFlags.DoNotUpdateTargetState | ConnectDriveFlags.NoTimeout); } catch (WNetApiException ex) { if (ex.IsCredentialError) { credentialErrors++; if (credentialErrors >= AskForCredentialsAfterFails) { this.log.Warn("Background task for drive \"" + drive.LocalDriveLetter + "\" stopped: " + credentialErrors + " credential errors have occured. Asking the user for a password again"); RenewDrivePassword(drive); // either RenewDrivePassword resolved the issue or the user aborted the activity drive.TargetConnectivity = drive.IsConnectedCached; return; } } else { credentialErrors = 0; if (!ex.IsTemporaryError) { this.log.Warn("Background task for drive \"" + drive.LocalDriveLetter + "\" stopped: " + ex.Message); drive.TargetConnectivity = drive.IsConnectedCached; return; } } // do not flood the log with the same error again and again and again and again and again and again and again and again and again if (ex.ErrorCode != lastErrorCode) { lastErrorCode = ex.ErrorCode; this.log.Error("Windows-API returned " + ex.ErrorCode + " for drive \"" + drive.LocalDriveLetter + "\": " + ex.Message); } } catch (Exception ex) { this.log.Error("Background task for drive \"" + drive.LocalDriveLetter + "\" stopped: " + ex.Message + " (" + ex.GetType().Name + ")", ex); // an error occured which will not be solved by just retrying drive.TargetConnectivity = false; return; } } else { // user changed it's mind and the drive has no longer to be connected this.log.Info("Background task for drive \"" + drive.LocalDriveLetter + "\" stopped: Drive was changed (ConnectOnStartup=" + drive.ConnectOnStartup + "; TargetConnectivity=" + drive.TargetConnectivity + ")"); // update target connectivity to prevent the continuous checking logic from connecting this drive again (and fail...) drive.TargetConnectivity = drive.IsConnectedCached; return; } } catch (ThreadAbortException) { return; } catch { } // empty outter error handler -> the drive MUST be unlocked to prevent deadlocks } while (!drive.IsConnected); this.log.Info("Drive \"" + drive.LocalDriveLetter + "\" connected after " + (Environment.TickCount - startTime) + " ms"); }