Dictionary <string, JpnEncoding> DetectEncodingOfEachFile(string[] arrPtnMtchPaths) { Dictionary <string, JpnEncoding> dicPtnMtchPathAndEnc = new Dictionary <string, JpnEncoding>(); for (int i = 0; i < arrPtnMtchPaths.Length; i++) { try { string path = arrPtnMtchPaths[i]; Progress.ReportProgress(0, "エンコーディングを調べています...\n" + path); Progress.CheckCanceledOrNot(); JpnEncoding jenc = JpnEncoding.DetectEncoding(path, JpnEncoding.Shift_JIS); if (jenc != null) { dicPtnMtchPathAndEnc.Add(path, jenc); } } catch (OperationCanceledException) { throw; } catch (Exception exception) { Progress.ReportError(exception); } } return(dicPtnMtchPathAndEnc); }
void DoFindBkgnd(object sender, DoWorkEventArgs eventArgs) { BackgroundWorker bw = sender as BackgroundWorker; try { Progress.ProgressUpdated += bw.ReportProgress; OperationFindOptions options = (_inclSubDir ? OperationFindOptions.IncludeSubDirectory : OperationFindOptions.None) | (_inclHiddenDir ? OperationFindOptions.IncludeHiddenDirectory : OperationFindOptions.None) | (_inclHiddenFile ? OperationFindOptions.IncludeHiddenFile : OperationFindOptions.None) | (_regEx ? OperationFindOptions.RegexEnabled : OperationFindOptions.None); OperationFind operationFind = new OperationFind( _dirPath, _pattern, _beforeText, options, JpnEncoding.NumberToJpnEncoding(_encNumber), _regExMultiline ? RegexOptions.Multiline : RegexOptions.Singleline, Progress); _fileListView = operationFind.ExecuteOperationFind(); } catch (Exception) { _fileListView.ListValid = false; throw; // RunWorkerCompleted event handler will be called } }
void WriteFiles(ListViewItem item) { string path = item.SubItems[FileListView.ClmnPathNumber].Text; int codepg = JpnEncoding.NameToJpnEncoding(item.SubItems[1].Text).Encoding.CodePage; Encoding enc = null; if (codepg == 65001) { // avoid Encoding.GetEncoding(65001) as it refers to UTF-8 with BOM enc = new UTF8Encoding(false); } else { enc = Encoding.GetEncoding(codepg); } string text; try { using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { using (StreamReader sr = new StreamReader(fs, enc, false)) // false is not specified in 1.3.0 { text = sr.ReadToEnd(); } } if (RegExEnabled) { Regex regex = new Regex(BeforeText, RegexOptions); text = regex.Replace(text, AfterText); } else // !RegExEnabled { text = text.Replace(BeforeText, AfterText); } using (FileStream fs = new FileStream(path, FileMode.Truncate, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs, enc)) { sw.Write(text); } } item.Checked = false; item.BackColor = FileListView.SuccessBackColor; } catch (OperationCanceledException) { throw; } catch (Exception exception) { item.BackColor = FileListView.FailureBackColor; Progress.ReportError(exception); } }
public OperationFind(string dirPath, string pattern, string content, OperationFindOptions options, JpnEncoding encoding, RegexOptions regexOptions, Progress progress) { DirPath = dirPath; Pattern = pattern; Content = content; Options = options; Encoding = encoding; RegexOptions = regexOptions; Progress = progress; }
public MainForm() { InitializeComponent(); InitializeMainMenu(); AssemblyName assemblyName = Assembly.GetExecutingAssembly().GetName(); this.Text = "Hoge Text Replace " + assemblyName.Version.Major + "." + assemblyName.Version.Minor; this.Icon = global::HogeTextReplace.Properties.Resources.ProgramIcon; for (int i = 0; i <= JpnEncoding.MaxNumber; i++) { comboBoxEnc.Items.Add(JpnEncoding.NumberToJpnEncoding(i).Name); } comboBoxEnc.SelectedIndex = 0; cbRegEx_CheckedChanged(null, null); cbSubDir_CheckedChanged(null, null); cbSubDir_EnabledChanged(null, null); InclSubDirCheckBoxEnabled = (BackupArgs.Mode != BackupMode.SubFolder); // to avoid backup files found comboBoxNewline.Items.AddRange(eolCodes); comboBoxNewline.SelectedIndex = 0; LoadTextContent(); // from text_before.ini and text_after.ini azukiBefore.ScrollToCaret(); azukiAfter.ScrollToCaret(); azukiBefore.Document.ClearHistory(); azukiAfter.Document.ClearHistory(); iniFile = new IniForMainForm("config.ini", this); iniFile.LoadIni(); }
void BackupFiles(ListViewItem item) { try { string sourcePath = item.SubItems[FileListView.ClmnPathNumber].Text; string destPath = BackupArgs.UserDefinedBackupFileName(sourcePath, BackupArgs.TextAsUserDefined()); string destDir = Path.GetDirectoryName(destPath); if (!Directory.Exists(destDir)) { Directory.CreateDirectory(destDir); } if (BackupArgs.AppendMode) { int codepg = JpnEncoding.NameToJpnEncoding(item.SubItems[1].Text).Encoding.CodePage; Encoding enc = null; if (codepg == 65001) { // avoid Encoding.GetEncoding(65001) as it refers to UTF-8 with BOM enc = new UTF8Encoding(false); } else { enc = Encoding.GetEncoding(codepg); } string text; using (FileStream fs = new FileStream(sourcePath, FileMode.Open, FileAccess.Read)) { using (StreamReader sr = new StreamReader(fs, enc)) { text = sr.ReadToEnd(); } DateTime dtNow = DateTime.Now; string eol = Environment.NewLine; string borderline = new string('-', sourcePath.Length > 20 ? sourcePath.Length : 20); // 20 is length of date string text = borderline + eol + text; text = sourcePath + eol + text; text = dtNow.ToString("yyyy-MM-dd HH:mm:ss") + eol + text; text = borderline + eol + text; text = eol + text + eol; } using (FileStream fs = new FileStream(destPath, FileMode.Append, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs, enc)) { sw.Write(text); } } } else { File.Copy(sourcePath, destPath, true); } } catch (OperationCanceledException) { throw; } catch (Exception exception) { Progress.ReportError(exception, "バックアップエラー: "); } }