txt文件获取字符串前后的内容

wubo_1989 2012-05-23 09:04:40
用的是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");
希望各位高手帮帮忙。
...全文
95 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wubo_1989 2012-05-23
  • 打赏
  • 举报
回复
都不行啊,会不会是我多加了个字节打乱了顺序。
dalmeeme 2012-05-23
  • 打赏
  • 举报
回复
StreamWriter sw = new StreamWriter(path1, false, System.Text.Encoding.UTF8);
或者
StreamWriter sw = new StreamWriter(path1, false, System.Text.Encoding.GetEncoding("GB2312"));

一般多数情况下用这两种。
EnForGrass 2012-05-23
  • 打赏
  • 举报
回复
指定为GB2312试试
StreamWriter sw = new StreamWriter(path1, false, System.Text.Encoding.GetEncoding("GB2312"));
wubo_1989 2012-05-23
  • 打赏
  • 举报
回复
自己先来个...标题写错了...

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧