弄了一个文本读写修改器,但是运行程序更改之后一直存在中文乱码问题,用 NOTEpad++ 打开显示是 但是用 Encoding.Default,Encoding.GetEncoding("uft-8"),Encoding.GetEncoding("")都试过了,但是都不行,麻烦各位指点一二。
代码,贴上 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO;
namespace frmModify { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private List<string> lines;</string>
private void label3_Click(object sender, EventArgs e) { } private void textBox2_TextChanged(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { } private void btnSelect_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { txtPath.Text = dialog.SelectedPath; } } private void btnModify_Click(object sender, EventArgs e) { if(txtPath.Text == string.Empty) { MessageBox.Show("请选择文件"); return; } string[] files = System.IO.Directory.GetFiles(txtPath.Text, "*.xml", SearchOption.AllDirectories); foreach(string file in files) { LoadFile(file); ReplaceText(); SaveFile(file); } MessageBox.Show("替换成功"); } private void LoadFile(string file) { lines = System.IO.File.ReadAllLines(file).ToList(); } private void SaveFile(stringfile) { System.IO.File.SetAttributes(file, FileAttributes.Normal); System.IO.File.WriteAllLines(file, lines, Encoding.GetEncoding("")); } private void ReplaceText() { foreach (string line in lines) { if (line.IndexOf("删除单据") > 0) { line.Replace("Type=\"Edit\"", "Type=\"Print\""); } } } } }]
