Problem with accents, C# App Console

Asked

Viewed 1,450 times

1

Read and save the file

 public void LereGravar()
    {
        Console.WriteLine($"{DateTime.Now.ToString()} - Processamento Iniciado, Lendo Arquivo");

        //Procura dentro da Pasta
        DirectoryInfo Dir = new DirectoryInfo(LerArquivos);
        FileInfo[] Files = Dir.GetFiles("*", SearchOption.AllDirectories);
        var conteudoArquivo = new List<ArquivoTxt>();
        foreach (System.IO.FileInfo fi in Dir.GetFiles())
        {
            using (StreamReader leitor = fi.OpenText()) 
            {
                while (!leitor.EndOfStream)
                {
                    var linha = new ArquivoTxt();
                    linha.Linha = leitor.ReadLine();
                    conteudoArquivo.Add(linha);
                }
            }

            StreamWriter sw = new StreamWriter(Carteira_Vendas, true, System.Text.Encoding.GetEncoding("UTF-8"));
            conteudoArquivo = conteudoArquivo.Where(_ => _.Linha.StartsWith("|") &&
                                                 _.Linha.Substring(0, 2) != "|O" &&
                                                 _.Linha.Substring(0, 2) != "|-" &&
                                                 _.Linha.Substring(0, 2) != "|C" &&
                                                 _.Linha.Substring(0, 2) != "|I" &&
                                                 _.Linha.Substring(0, 2) != "|T" &&
                                                 _.Linha.Substring(0, 2) != "| " &&
                                                 _.Linha.Substring(0, 2) != "|R" &&
                                                 _.Linha.Substring(0, 2) != "|D" &&
                                                 _.Linha.Substring(0, 2) != "|E").ToList();
            Console.WriteLine($"{DateTime.Now.ToString()} - Processamento finalizado");

            foreach (var item in conteudoArquivo)
            {
                string[] linha = item.Linha.Split('|');
                string ordemVenda = linha[1].Trim();
                sw.WriteLine(TiraAcentuacao(item.Linha));
            };
            sw.Close();
        }
    }

You should remove the seats

        static string TiraAcentuacao(string StringAcentuada)
    {
        if (string.IsNullOrEmpty(StringAcentuada.Trim()))
            return string.Empty;
        else
        {
            byte[] array = Encoding.GetEncoding("iso-8859-8").GetBytes(StringAcentuada);
            return Encoding.UTF8.GetString(array);
        }
    }

Return of the Line I receive

|2021552949 |000018 |0001 |MTO |ZNOR |1182042 |3A ALUMINIO INDUSTRIA E COMERCIO EI|1182042 |3A ALUMINIO INDUSTRIA E COMERCIO EI|6155211 |ES E13152 6063 T5 6000 N-CG |4500017089 | 1.381,000 |KG | 3.908,230 | 3.908,230 | 0,000 |CN | 2.614,375 | 0,000 | 0,000 | 0,000 |26.12.2017 |2020 |C |30.06.2018|24.08.2018 |24.08.2018 |22.08.2018 |23.08.2018 | |2100 |OUTROS CC |ESN0300 |EXTRUDADO NATURAL |Conjuga?? o Load | |SIM |Extruded |BLEANDROGG |2SP156 |SP-ALUM? NIO/SP-JANDIRA |CIF-Jandira - SP | 0,000 | 6.000,00|2020 |

My return is (?) in the seats of the seats, what could I do for him to record the accent or simply ignore it.

  • If you just do the sw.WriteLine(item.Linha); does not record with accents?

  • No, I’ve tried, he replaces the seat for a question mark.

  • I did the test here and it worked normal. I don’t know much about encoding, but maybe it’s this transition that’s causing the problem. Try using this method: https://dotnetfiddle.net/3VwlPV

1 answer

1


Experiment as follows:

using System.Text;

public void LereGravar()
{
    Console.WriteLine($"{DateTime.Now.ToString()} - Processamento Iniciado, Lendo Arquivo");

    var files = Directory.GetFiles("LerArquivos", "*", SearchOption.AllDirectories);
    var conteudoArquivo = new List<ArquivoTxt>();

    foreach (var file in files)
    {
        using (StreamReader leitor = new StreamReader(file, Encoding.GetEncoding("ISO-8859-1")))
        {
            while (!leitor.EndOfStream)
            {
                var linha = new ArquivoTxt() { Linha = leitor.ReadLine() };
                conteudoArquivo.Add(linha);
            }
        }

        using (StreamWriter sw = new StreamWriter(Carteira_Vendas, true, Encoding.GetEncoding("ISO-8859-1")))
        {
            conteudoArquivo = conteudoArquivo.Where(_ => _.Linha.StartsWith("|") &&
                                                 _.Linha.Substring(0, 2) != "|O" &&
                                                 _.Linha.Substring(0, 2) != "|-" &&
                                                 _.Linha.Substring(0, 2) != "|C" &&
                                                 _.Linha.Substring(0, 2) != "|I" &&
                                                 _.Linha.Substring(0, 2) != "|T" &&
                                                 _.Linha.Substring(0, 2) != "| " &&
                                                 _.Linha.Substring(0, 2) != "|R" &&
                                                 _.Linha.Substring(0, 2) != "|D" &&
                                                 _.Linha.Substring(0, 2) != "|E").ToList();
            Console.WriteLine($"{DateTime.Now.ToString()} - Processamento finalizado");

            foreach (var item in conteudoArquivo)
            {
                string[] linha = item.Linha.Split('|');
                string ordemVenda = linha[1].Trim();
                sw.WriteLine(item.Linha);
            }
        }
    }
}

I changed your code a little bit, I think there’s no need to use the FileInfo when you can open the file from the Path.

The most "critical" amendment was to use the StreamReader with the Encoding.GetEncoding("ISO-8859-1"), as well as the StreamWriter.

  • It didn’t work keeps returning me without accentuating in place the "?"

  • In the original file the accents are correct or comes with the character "?"?

  • is correct in the original, when rewriting it is without accentuation.

  • For, the UTF-8 does not treat the accents correctly. Answer edited to instead of using the UTF-8 use the ISO-8859-1.

  • Don’t forget to give the UP in the answer ;)

  • It worked, thank you!!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.