c# 保存文本文件 出现奇怪字符

来源:百度知道 编辑:UC知道 时间:2024/06/22 03:49:43
我用c#写了一个东西,用来下载网站上的一些CSS文件,但是这些css文件保存到本地了以后就会出现奇怪的符号.
比如很多该是回车符的地方就出现了黑色的方块。
我尝试过用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;