public FileListView ExecuteOperationReplace() { foreach (ListViewItem item in FileListView.Items) { if (!item.Checked) { continue; } string path = item.SubItems[FileListView.ClmnPathNumber].Text; if (!string.IsNullOrEmpty(BackupArgs.TextAsUserDefined())) { Progress.ReportProgress(0, "バックアップファイルを作成しています...\n" + path); Progress.CheckCanceledOrNot(); BackupFiles(item); } Progress.ReportProgress(0, "ファイルを置換しています...\n" + path); Progress.CheckCanceledOrNot(); WriteFiles(item); } Progress.ReportProgress(0, "ファイルリストを更新しています...\n"); Progress.CheckCanceledOrNot(); return(UpdateListView()); }
private void tbUserDefinition_TextChanged(object sender, EventArgs eventArgs) { string tmp_path = @"c:\" + "sample.txt"; string result = BackupArgs.UserDefinedBackupFileName(tmp_path, tbUserDefinition.Text); result = result.Replace(@"c:\", ""); lbUserDefinitionSampleResult.Text = result; }
public OperationReplace(FileListView fileListView, string before, string after, bool regex, RegexOptions regexOptions, Progress progress, BackupArgs backupArgs) { FileListView = fileListView; BeforeText = before; AfterText = after; RegExEnabled = regex; RegexOptions = regexOptions; BackupArgs = backupArgs; Progress = progress; }
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, "バックアップエラー: "); } }