ASP.NET Stream 读写html 中文乱码问题

做东西的时候遇见Stream 读写html 中文乱码的问题  解决:在读取和写入的时候编码统一  可能这样还不能解决问题 可以配置依稀webconfig使编码统一:

<system.web>
    <globalization requestEncoding="gb2312" responseEncoding="gb2312"/>

</system.web>

/// <summary>
    /// 文件内容读取
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    public string FileStreamReader(string filePath)
    {
        if (!File.Exists(filePath))
        {
            return "";
        }
        else
        {
            StreamReader streamReader = new StreamReader(filePath, System.Text.Encoding.GetEncoding("gb2312"));
            string strContent = streamReader.ReadToEnd();
            streamReader.Close();
            streamReader.Dispose();
            return strContent;
        }
    }

    /// <summary>
    /// 文件内写入
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    public bool FileStreamWrite(string filePath, string content)
    {
        if (!File.Exists(filePath))
        {
            return false;
        }
        else
        {
            try
            {
                StreamWriter streamWriter = new StreamWriter(filePath,false,System.Text.Encoding.GetEncoding("gb2312"));
               streamWriter.Write(content);
                streamWriter.Close();
                streamWriter.Dispose();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

 

你可能感兴趣的:(asp.net)