Beispiel #1
0
            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);
                }
            }
Beispiel #2
0
            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, "バックアップエラー: ");
                }
            }