private void btnCheck_Click(object sender, RoutedEventArgs e) { if (!CheckInput()) { return; } LoadingWindow.TryShowLoadingWindow(Properties.Strings.ProgressMessageCheckingConnection, new Action(() => { // try connect Exception ex; if (!this.networkDrive.CheckConnection(out ex)) { log.Error("Connection check of \"" + this.networkDrive.LocalDriveLetter + "\" to \"" + this.networkDrive.ExpandedRemoteAddress + "\" with user \"" + this.networkDrive.Username + "\" failed: " + ex.Message + " (" + ex.GetType().Name + ")", ex); this.Dispatcher.Invoke(new Action(() => { MessageBox.Show(LoadingWindow.GetStaticLoadingWindow(), string.Format(Properties.Strings.MessageCouldNotConnect, this.networkDrive.LocalDriveLetter, Helper.GetUserMessage(ex)), Properties.Strings.TitleCheckConnection, MessageBoxButton.OK, MessageBoxImage.Exclamation); })); } else { this.Dispatcher.Invoke(new Action(() => { MessageBox.Show(LoadingWindow.GetStaticLoadingWindow(), Properties.Strings.MessageConnectionSuccessfull, Properties.Strings.TitleCheckConnection, MessageBoxButton.OK, MessageBoxImage.Information); })); } }), this); }
private bool CheckNetworkDrive(NetworkDrive drive) { bool result = false; LoadingWindow.TryShowLoadingWindow(Properties.Strings.ProgressMessageCheckingConnection, new Action(() => { Exception ex; result = drive.CheckConnection(out ex); if (!result) { log.Error("Connection check of \"" + drive.LocalDriveLetter + "\" to \"" + drive.ExpandedRemoteAddress + "\" with user \"" + drive.Username + "\" failed: " + ex.Message + " (" + ex.GetType().Name + ")", ex); App.Current.Dispatcher.Invoke(new Action(() => { MessageBox.Show(LoadingWindow.GetStaticLoadingWindow(), string.Format(Properties.Strings.MessageCouldNotConnect, drive.LocalDriveLetter, Helper.GetUserMessage(ex)), Properties.Strings.TitleCheckConnection, MessageBoxButton.OK, MessageBoxImage.Exclamation); })); } })); return(result); }
private void btnOK_Click(object sender, RoutedEventArgs e) { if (cbxUseEncryption.IsChecked == true) { if (tbxEncryptionPassword.Password != tbxEncryptionPasswordRepeat.Password) { MessageBox.Show(Strings.MessagePasswordsDoNotMatch, this.Title, MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } if (string.IsNullOrEmpty(tbxEncryptionPassword.Password)) { MessageBox.Show(Strings.ExportWindowNoPasswordMessage, this.Title, MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } } if (cbxStorePassword.IsChecked == true && cbxUseEncryption.IsChecked != true) { MessageBox.Show(Strings.ExportWindowPasswordWithoutEncryptionMessage, this.Title, MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } SaveFileDialog dialog = new SaveFileDialog(); dialog.DefaultExt = ".drives"; dialog.Title = this.Title; dialog.Filter = "*.drives|*.drives"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { bool storeUsername = (cbxStoreUsername.IsChecked == true); bool storePassword = (cbxStorePassword.IsChecked == true); bool useEncryption = (cbxUseEncryption.IsChecked == true); string password = tbxEncryptionPassword.Password; bool close = false; LoadingWindow.TryShowLoadingWindow(Strings.MainWindowExportProgressMessage, new Action(() => { try { JArray drivesList = new JArray(); foreach (NetworkDrive drive in this.exportDrives) { JObject obj = drive.ExportToJson(); if (!storeUsername) { // remove credentials obj["Username"] = null; obj["Password"] = null; } else if (storePassword) { // write unencrypted password to file obj["Password"] = drive.GetPassword(); } else { obj["Password"] = null; } if (useEncryption) { foreach (KeyValuePair <string, JToken> kvp in obj) { if (kvp.Value.Type == JTokenType.String) { obj[kvp.Key] = Helper.EncryptValue(kvp.Value.Value <string>(), password); } } } drivesList.Add(obj); } JObject exportObj = new JObject(); exportObj.Add("IsEncrypted", useEncryption); if (useEncryption) { // save salted checksum of the password to check for the correct password exportObj.Add("Check", Helper.GeneratePasswordStore(password)); } exportObj.Add("Drives", drivesList); log.Info("Export " + drivesList.Count + " drives to \"" + dialog.FileName + "\""); File.WriteAllText(dialog.FileName, exportObj.ToString(), Encoding.UTF8); close = true; } catch (Exception ex) { log.Error("Unable to export drives: " + ex.Message + " (" + ex.GetType().Name + ")", ex); this.Dispatcher.Invoke(new Action(() => { MessageBox.Show(LoadingWindow.GetStaticLoadingWindow(), string.Format(Strings.MainWindowExportErrorMessage, ex.GetType().Name, ex.Message, ex.StackTrace), Strings.TitleCheckConnection, MessageBoxButton.OK, MessageBoxImage.Exclamation); })); } })); if (close) { this.DialogResult = true; Close(); } } }
private void Application_Startup(object sender, StartupEventArgs e) { int startTime = Environment.TickCount; try { this.applicationPath = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]); if (!Branding.IsInitialized) { MessageBox.Show("Fatal error during initialization. Please reinstall the application.", "Network Drive Service", MessageBoxButton.OK, MessageBoxImage.Error); Shutdown(-1); return; } GlobalContext.Properties["AppDataPath"] = Branding.AppDataPath; XmlConfigurator.Configure(); this.log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); this.log.Info("Startup"); #if !DEBUG //TODO workaround for SD-163 => find root cause... AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException; Application.Current.Dispatcher.UnhandledException += Dispatcher_UnhandledException; #endif } catch (Exception ex) { MessageBox.Show(ex.GetType().Name + " caught during initialization:\r\n\r\n" + ex.Message, "Network Drive Service", MessageBoxButton.OK, MessageBoxImage.Error); Shutdown(-1); return; } try { this.log.Info("-> " + Branding.ApplicationName + ", Version " + SoftwareVersion); } catch (Exception ex) { this.log.Warn("Could not retrieve version number (" + ex.GetType().Name + "): " + ex.Message, ex); } this.appOptions = new ApplicationOptions(); // override system language if (!string.IsNullOrEmpty(this.appOptions.SelectedLanguage)) { try { /* #### IMPORTANT: needs to be done in STA thread! #### */ Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(this.appOptions.SelectedLanguage); Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(this.appOptions.SelectedLanguage); } catch (Exception ex) { this.log.Error("Could not set language to \"" + this.appOptions.SelectedLanguage + " (" + ex.GetType().Name + "): " + ex.Message, ex); } } string systemLangName = ""; try { systemLangName = Thread.CurrentThread.CurrentUICulture.Name; // load translated error messages to the cache WNetApiException.UpdateErrorCodes(); } catch (Exception ex) { this.log.Warn("Could not retrieve language (" + ex.GetType().Name + "): " + ex.Message, ex); } try { // is this the first instance? IpcChannelName = IpcChannelPrefix + Environment.UserDomainName + "." + Environment.UserName; bool isMutexOwner; this.applicationMutex = new Mutex(true, IpcChannelName, out isMutexOwner); if (e.Args.Length > 0 && e.Args[0] == "-wait") { if (!isMutexOwner) { log.Info("-> Wait for other instances to close..."); do { this.applicationMutex.Close(); Thread.Sleep(200); this.applicationMutex = new Mutex(true, IpcChannelName, out isMutexOwner); } while (!isMutexOwner); } } if (!isMutexOwner) { log.Info("-> Another instance is already running"); if (e.Args.Length >= 1 && e.Args[0] == "-exit") { // send close command to other applications try { NamedPipeClientStream ipcClient = new NamedPipeClientStream(".", IpcChannelName, PipeDirection.Out, PipeOptions.Asynchronous); ipcClient.Connect(); ipcClient.WriteByte((byte)IpcCommands.Exit); } catch (Exception ex) { log.Error("Could not send 'Exit' on Ipc Channel: " + ex.Message + " (" + ex.GetType().Name + ")", ex); } } else { // send show-window command to other application try { NamedPipeClientStream ipcClient = new NamedPipeClientStream(".", IpcChannelName, PipeDirection.Out, PipeOptions.Asynchronous); ipcClient.Connect(); ipcClient.WriteByte((byte)IpcCommands.ShowMainWindow); } catch (Exception ex) { log.Error("Could not send 'ShowMainWindow' on Ipc Channel: " + ex.Message + " (" + ex.GetType().Name + ")", ex); } } // only one instance allowed -> close this instance Shutdown(); return; } bool initSuccess = false; if (e.Args.Length > 0 && e.Args[0] == "-hidden") { initSuccess = InitApplication(systemLangName); } else { // disable shutdown on last window closed. otherwise closing the loading window would shutdown the application App.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown; LoadingWindow.TryShowLoadingWindow(Strings.MessageInitialization, new Action(() => { initSuccess = InitApplication(systemLangName); })); // re-enable to return to normal behaviour App.Current.ShutdownMode = ShutdownMode.OnMainWindowClose; } if (!initSuccess) { Shutdown(-1); return; } this.log.Info("Startup took " + (Environment.TickCount - startTime) + " ms"); } catch (Exception ex) { this.log.Error("Unhandled Exception in App.Application_Startup: " + ex.Message + " (" + ex.GetType().Name + ")", ex); MessageBox.Show(string.Format(Strings.MessageFatalError, ex.GetType().Name, ex.Message, ex.StackTrace), Branding.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Error); Shutdown(-1); } }