C# Help reading foreign characters using StreamReader

后端 未结 10 1262
别那么骄傲
别那么骄傲 2020-11-29 02:46

I\'m using the code below to read a text file that contains foreign characters, the file is encoded ANSI and looks fine in notepad. The code below doesn\'t work, when the fi

相关标签:
10条回答
  • 2020-11-29 03:22

    I solved my problem of reading portuguese characters, changing the source file on notepad++.

    enter image description here

    C#

        var url = System.Web.HttpContext.Current.Server.MapPath(@"~/Content/data.json");
        string s = string.Empty;
        using (System.IO.StreamReader sr = new System.IO.StreamReader(url, System.Text.Encoding.UTF8,true))
        {
              s = sr.ReadToEnd();
        }
    
    0 讨论(0)
  • 2020-11-29 03:25

    Yes, it could be with the actual encoding of the file, probably unicode. Try UTF-8 as that is the most common form of unicode encoding. Otherwise if the file ASCII then standard ASCII encoding should work.

    0 讨论(0)
  • 2020-11-29 03:25

    Try a different encoding such as Encoding.UTF8. You can also try letting StreamReader find the encoding itself:

        StreamReader reader = new StreamReader(inputFilePath, System.Text.Encoding.UTF8, true)
    

    Edit: Just saw your update. Try letting StreamReader do the guessing.

    0 讨论(0)
  • 2020-11-29 03:25

    I'm also reading an exported file which contains french and German languages. I used Encoding.GetEncoding("iso-8859-1"), true which worked out without any challenges.

    0 讨论(0)
提交回复
热议问题