txt文件获取字符串前后的内容
用的是VS2005,需要对个txt文件进行修改后保存,可以是修改保存完后发现txt里面的中文全成了乱码,代码如下:
string path = @"E:\\main.txt";
string path1 = @"E:\\main1.txt";
if (File.Exists(path))
{
StreamReader sr = new StreamReader(path);
string stra = sr.ReadToEnd();
sr.Close();
stra = stra.Replace("VMCD", ";VMCD");
StreamWriter sw = new StreamWriter(path1, false, System.Text.Encoding.Default);
sw.Write(stra);
sw.Close();
}
else
{
MessageBox.Show("the txt can't find");
希望各位高手帮帮忙。
------解决方案--------------------指定为GB2312试试
StreamWriter sw = new StreamWriter(path1, false, System.Text.Encoding.GetEncoding("GB2312"));
------解决方案--------------------StreamWriter sw = new StreamWriter(path1, false, System.Text.Encoding.UTF8);
或者
StreamWriter sw = new StreamWriter(path1, false, System.Text.Encoding.GetEncoding("GB2312"));
一般多数情况下用这两种。