Silverlight读取包含中文的txt(解决乱码问题)

Silverlight读取包含中文的txt(解决乱码问题),第1张

概述虽然Silverlight已经是被抛弃的孩子了,但此前做的一些项目还是用到Silverlight。今天用Silverlight导入客户端本地的一个txt文件,然后读取需要的信息。随手记录一下,留作以后备忘。 网上也有不少的相关资料,但好像要么是单纯的Ctrl+C、Ctrl+V,要么是不够齐全。闲话少说,直接上代码。 我用的是MVVM模式,所以用RelayCommand命令打开了。其它的,直接在类似

虽然Silverlight已经是被抛弃的孩子了,但此前做的一些项目还是用到Silverlight。今天用Silverlight导入客户端本地的一个txt文件,然后读取需要的信息。随手记录一下,留作以后备忘。@H_502_8@

网上也有不少的相关资料,但好像要么是单纯的Ctrl+C、Ctrl+V,要么是不够齐全。闲话少说,直接上代码。@H_502_8@

我用的是MVVM模式,所以用RelayCommand命令打开了。其它的,直接在类似button_Click事件里调用Openfile(),同理。@H_502_8@

 
        /// <summary>         /// 打开文件         /// </summary>         private voID Openfile()         {             try             {                 //检查是否是在OOB模式下运行                 OOBChecker.CheckInstallState(() =>                 {                     //d出选择文件对话框                     OpenfileDialog fileDialog = new OpenfileDialog()                     {                         Filter = "Txt files (*.txt)|*.txt|All files (*.*)|*.*",};                      if (fileDialog.ShowDialog() == true)                     {                         //读取文件流                         using (Stream fs = fileDialog.file.OpenRead())                         {                             ReadTxtfile(fs);                             fs.Close();                         }                     }                 });             }             catch (Exception e)             {                  //错误处理             }         }

注: OOBChecker.CheckInstallState,是为公司项目需要写的一个检查Silverlight程序是否在浏览器外运行的方法。众所周知,Silverlight是可以安装在本机脱离浏览器运行的,这样便可获得更高的执行权限。有时对本地文件(夹)进行 *** 作时,程序需要获得信任权限。不知是否我浏览器设置问题,我直接在IE上运行,貌似也有权限。@H_502_8@

附上OOBChecker的代码。@H_502_8@

@H_502_8@

   public class OOBChecker        {            /// <summary>            /// 当前程序是否安装在本地            /// </summary>            private static bool IsInstall { get; set; }            /// <summary>            /// 检查当前程序有没有安装在本地,callback是回调            /// </summary>            public static voID CheckInstallState(Action callback)            {                try                {                    if (App.Current.HasElevatedPermissions)                    {                        //跳过OOB验证,直接在网页打开                        if (callback != null)                        {                            callback();                            return;                        }                    }                    if (App.Current.InstallState == InstallState.Installed)                    {                        if (App.Current.IsRunningOutOfbrowser)                        {                            IsInstall = true;                            //项目中 *** 作Office COM需要                            if (automationFactory.IsAvailable)                            {                                if (callback != null)                                {                                    callback();                                }                            }                            else                            {                                MessageWindow.CreateNew("您好,automationFactory IsAvailable Is False",resTipinformation.strApplicationTitle);                            }                            //App.Current.MainWindow.topMost = true;                        }                        else                        {                            MessageWindow.CreateNew("您好,您已在本机安装了模块程序,请保存本页面数据后双击运行桌面的图标。",resTipinformation.strApplicationTitle);                        }                    }                    else                    {                        MessageWindow.CreateNewConfirm("打开文档之前需要安装本程序在您电脑上,确定帮您安装吗?为避免数据丢失,请确认前先保存数据。",resTipinformation.strApplicationTitle,() =>                            {                                App.Current.InstallStateChanged += Current_InstallStateChanged;                                App.Current.Install();                            });                    }                }                catch (Exception ex)                {                    ErrorWindow.CreateNew(ex);                    DeBUG.Writeline(string.Format("{0} at {1}",ex.Message,"OOBChecker"));                }            }            /// <summary>            /// 安装完成后            /// </summary>            /// <param name="sender"></param>            /// <param name="e"></param>            private static voID Current_InstallStateChanged(object sender,System.EventArgs e)            {                if (App.Current.InstallState == InstallState.Installed)                {                    IsInstall = true;                }            }        }

VIEw Code

 @H_502_8@

Openfile方法中,调用了一个读取文件内容的方法:ReadTxtfile@H_502_8@

         /// <summary>         /// 一行行地循环读取         /// </summary>         private voID ReadTxtfile(Stream fs)         {             var mytable = new List<string>();             using (StreamReader reader = new StreamReader(fs,new Gb2312EnCoding()))             {                 /*                  * 一次性读出所有文件信息                  * var fileText = string.Empty;                 fileText = reader.ReadToEnd();                  */                  reader.BaseStream.Seek(0,SeekOrigin.Begin);                 string strline = reader.Readline();                 var tableIndex = 0;                 while (strline != null)                 {                     /*                      * do your things here                     if (strline.Contains("[Your message is here]"))                     {                         //找到表头了                         tableIndex++;                     }                      if (tableIndex > 6)                     {                         //读完需要的表信息了,跳出                         break;                     }                      if (tableIndex >= 4)                     {                         //开始获取需要的信息                         mytable.Add(strline);                     }                     if (tableIndex > 0)                     {                         tableIndex++;                     }                     */                     strline = reader.Readline();                 }                 reader.Close();             }              if (mytable.Any())             {                 foreach (var strItem in mytable)                 {                     var vIEw = strItem.TrimEnd('\t').Split('\t');                     //do something here                 }             }          }


注:Silverlight不支持GB2312编码,所以打开中文时会乱码。为此,我们可以继承EnCoding,自己为程序添加GB2312编码。然后像下面那样调用。@H_502_8@

StreamReader reader = new StreamReader(fs,new Gb2312EnCoding()) 
Gb2312EnCoding的代码,里面包含了个Gb2312toUnicodeDictinary(这个是网上找的,很多网上资料只有Gb2312EnCoding,却没有附上Gb2312toUnicodeDictinary)。只上Gb2312EnCoding的代码吧,Gb2312toUnicodeDictinary有7000多行的说……,这两个文件以附件形式放在后面,有需要的童鞋自行下载。

@H_502_8@

  public class Gb2312EnCoding : EnCoding     {         public overrIDe string Webname         {             get             {                 return "gb2312";             }         }          public Gb2312EnCoding()         {          }         public overrIDe int GetBytes(char[] chars,int charIndex,int charCount,byte[] bytes,int byteIndex)         {             throw new NotImplementedException();         }          public overrIDe int GetChars(byte[] bytes,int byteIndex,int byteCount,char[] chars,int charIndex)         {             int j = 0;             char c;             for (int i = 0; i < byteCount; i += 2)             {                 if (i + 1 >= bytes.Length)                 {                     char[] last = EnCoding.UTF8.GetChars(new byte[] { bytes[i] });                     chars[j] = last[0];                 }                 else                 {                     byte[] bb = new byte[] { bytes[i],bytes[i + 1] };                     if (Gb2312toUnicodeDictinary.TryGetChar(bb,out c))                     {                         chars[j] = c;                         j++;                     }                     else                     {                         char[] tt = EnCoding.UTF8.GetChars(new byte[] { bb[1] });                         chars[j] = tt[0];                         j++;                         //测试下一个                         if (i + 2 >= bytes.Length)                         {                             char[] tttt = EnCoding.UTF8.GetChars(new byte[] { bb[0] });                             chars[j] = tttt[0];                             j++;                         }                         else                         {                             byte[] test = new byte[] { bb[0],bytes[i + 2] };                             if (Gb2312toUnicodeDictinary.TryGetChar(test,out c))                             {                                 chars[j] = c;                                 j++;                                 i++;                             }                             else                             {                                 char[] ttt = EnCoding.UTF8.GetChars(new byte[] { bb[0] });                                 chars[j] = ttt[0];                                 j++;                             }                          }                     }                 }             }              return chars.Length;         }          public overrIDe int GetByteCount(char[] chars,int index,int count)         {             return count;         }         public overrIDe int GetCharCount(byte[] bytes,int count)         {             return count;         }          public overrIDe int GetMaxByteCount(int charCount)         {             return charCount;         }         public overrIDe int GetMaxCharCount(int byteCount)         {             return byteCount;         }         public static int CharacterCount         {             get { return 7426; }         }     }

VIEw Code

 @H_502_8@

 附件(GB2312Encoding+GB2312toUnicodeDictinary)下载@H_502_8@   总结

以上是内存溢出为你收集整理的Silverlight读取包含中文的txt(解决乱码问题)全部内容,希望文章能够帮你解决Silverlight读取包含中文的txt(解决乱码问题)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1015292.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存