private void OpenFile(string fileName, bool confirm = true) { try { var text = string.Empty; if (Regex.IsMatch(fileName, "\\.txt$", RegexOptions.IgnoreCase)) { text = File.ReadAllText(fileName, Encoding.Default); } else if (Regex.IsMatch(fileName, "\\.rtf$", RegexOptions.IgnoreCase)) { if (confirm && !SettingViewModel.Instance.RemeberRTF) { var confirmWindow = new ConfirmWindow(); confirmWindow.Title = "提示"; confirmWindow.MessageTextBlock.Text = "打开 RTF 格式的文件将丢失原有格式信息、和图片等对象,是否需要继续?"; confirmWindow.RemeberCheckedBox.Content = "下次不再提示"; confirmWindow.Owner = this; var result = confirmWindow.ShowDialog(); SettingViewModel.Instance.RemeberRTF = confirmWindow.RemeberCheckedBox.IsChecked == true; SettingViewModel.Instance.SaveRememberRTF(); if (result != true) { return; } } text = RtfHelper.Read(fileName); } else // 其他 { text = File.ReadAllText(fileName, Encoding.Default); } TypingSpeedViewModel.Instance.Total(); this.MainTextBox.Text = text; TypingSpeedViewModel.Instance.Refresh(); if (string.Compare(fileName, Common.TempFile, true) != 0) { MainViewModel.Instance.FileName = fileName; } OriginWords = MainViewModel.Instance.CountWords(MainTextBox.Text); SetFocus(true); } catch (Exception ex) { ShowMessage("打开文件({0})出现异常:{1}", fileName, ex.Message); } }
protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { SettingViewModel.Instance.RecentFile = MainViewModel.Instance.FileName; try { if (string.IsNullOrEmpty(MainViewModel.Instance.FileName)) { if (Words > 0) { SaveFileAs(); } } else { if (!SettingViewModel.Instance.AutoSaveExit) { var confirmWindow = new ConfirmWindow(); confirmWindow.Title = "提示"; confirmWindow.MessageTextBlock.Text = string.Concat(GetSaveCaption(), "\r\n\r\n是否保存当前文档?"); confirmWindow.RemeberCheckedBox.Content = "以后退出时自动保存当前文件"; confirmWindow.Topmost = true; var result = confirmWindow.ShowDialog(); if (result != true) { base.OnClosing(e); return; } SettingViewModel.Instance.AutoSaveExit = confirmWindow.RemeberCheckedBox.IsChecked == true; SettingViewModel.Instance.SaveAutoSaveExit(); } SaveFileTo(MainViewModel.Instance.FileName); } } catch (Exception ex) { ShowMessage("保存文件出现异常:", ex.Message); } base.OnClosing(e); }