asp.net怎么解决文本中文乱码的问题-创新互联

本篇内容介绍了“asp.net怎么解决文本中文乱码的问题”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

从网站建设到定制行业解决方案,为提供成都做网站、成都网站设计服务体系,各种行业企业客户提供网站建设解决方案,助力业务快速发展。创新互联公司将不断加快创新步伐,提供优质的建站服务。
 /// 
        /// 解决文本中文乱码   各类文本编码转UTF-8 文本
        /// 
        /// 原路径
        /// 临时保存路径
        /// 新的文件路径
        /// 新文件路径
        public static string  GetUtf8(string fullPath,string savePath,string strnew)
        {
                             Encoding e =GetFileEncodeType(fullPath);
                                System.IO.File.Create(savePath).Close();
                     using (StreamReader sr = new StreamReader(fullPath, e, false))
                                {
                              string utf8info = string.Empty;
                            Encoding utf8 = Encoding.UTF8;
                             // Convert the string into a byte[]. 
                           byte[] unicodeBytes = e.GetBytes(sr.ReadToEnd());
                           // Perform. the conversion from one encoding to the other. 
                           byte[] asciiBytes = Encoding.Convert(e, utf8, unicodeBytes);
                         // Convert the new byte[] into a char[] and then into a string. 
                        // This is a slightly different approach to converting tillustrate 
                      // the use of GetCharCount/GetChars. 
        char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
         utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
         utf8info = new string(asciiChars);
         using (StreamWriter sw = new StreamWriter(savePath, false, Encoding.UTF8))
                                    {
                                        sw.Write(utf8info);
                                    }
                                }
                                System.IO.File.Delete(fullPath);
                                System.IO.File.Copy(savePath,strnew,true);
                                System.IO.File.Delete(savePath);
                                return strnew;
        }
方法2
                    using (FileStream fs = new FileStream(fullPath, FileMode.Open))
                                {
                           using (StreamReader sr = new StreamReader(fs,Encoding.Default))
                                    {
                                        while (!sr.EndOfStream)
                                        {
                                            string sLine = sr.ReadLine();
                                            //StringBuilder b = new StringBuilder();
                                            //b.AppendLine(sLine);
                                            //另存到其他txt文件中   
                using (StreamWritersw=System.IO.File.AppendText(savepath))
                                            {
                                                //string n = b.ToString();
                                                sw.WriteLine(sLine.ToString());
                                            }
                                        }
                                    }
                                }
                                System.IO.File.Delete(fullPath);
                                string strNewFile = fullPath;
                                System.IO.File.Copy(savepath, strNewFile, true);
                                System.IO.File.Delete(savepath);
           //using (StreamReader sr = new StreamReader(strNewFile,Encoding.Default, false))
                                //{
              //    using (StreamWriter sw = new StreamWriter(fullPath, falsEncoding.UTF8))
                                //    {
                                //        sw.Write(Server.HtmlEncode(sr.ReadToEnd()));
                                //    }
                                //}
                                //System.IO.File.Delete(strNewFile);
                                FileInfo fileNew = new FileInfo(strNewFile);
                                sourcesFile.File_Size = fileNew.Length;
                                this.sourcesFileService.Save(sourcesFile);
                            }

“asp.net怎么解决文本中文乱码的问题”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享标题:asp.net怎么解决文本中文乱码的问题-创新互联
文章分享:http://ybzwz.com/article/dpchgg.html