c# 保存文本文件 出现奇怪字符
来源:百度知道 编辑:UC知道 时间:2024/09/23 13:28:05
比如很多该是回车符的地方就出现了黑色的方块。
我尝试过用gb2312 来保存,也是同样的效果,应该怎么做?
//我的代码大致如下
WebClient myWebClient = new WebClient();
myWebClient.Encoding = Encoding.Default;//.GetEncoding("gb2312");
myWebClient.DownloadFile(downLoadPath, path + filename);
希望高手指点
编码方式一至就不会出现乱码
看看下载的网站的编码方式先
用utf-8试试
给你一段保存txt文件的代码
//通过获得网址返回网页的源代码
public string WebClientGetSource(string url)
{
string source = "";
try
{
WebClient client = new WebClient();
Stream data = client.OpenRead(url);
//StreamReader reader = new StreamReader(data, Encoding.GetEncoding("GB2312"));
StreamReader reader = new StreamReader(data, Encoding.GetEncoding("GB2312"));
string strTemp = "";
strTemp = reader.ReadToEnd();
Application.DoEvents();
//while ((strTemp = reader.ReadLine()) != null)
//{
// source += strTemp + "\r\n";
//}
source = strTemp;