Ir para conteúdo
  • Cadastre-se

dev botao

Ler o arquivo INI de retorno da ACBrLib com C#


Ver Solução Respondido por Juliomar Marchetti,
  • Este tópico foi criado há 1605 dias atrás.
  • Talvez seja melhor você criar um NOVO TÓPICO do que postar uma resposta aqui.

Recommended Posts

  • Membros Pro
Postado

Bom dia, 

Gostaria de saber se alguém já criou algum método em C# que faça a leitura dos arquivos de retorno (.INI) dos métodos da ACBrLib, se sim poderia dar um help de como fizeram isso, pois preciso ler o retorno da NFE_Enviar e estou tendo algumas dificuldades para isso.

  • Membros Pro
Postado

Olá Antonio Carlos, sim estou com o Demo em C# para entender o funcionamento de todos os métodos, porém ele não lê nenhum retorno, ele apenas paga o resultado e apresenta em um textbox na tela, mas na realidade seria necessário ler o retorno validar o que ocorreu e ai programar a ação do sistema. 

Exemplo:  Msg=Nota(s) não confirmadas:
8->532-Rejeicao: Total do ICMS difere do somatorio dos itens

Esse foi o retorno no INI de uma NFCe que não foi aprovado, preciso ler que ele não obteve sucesso e informar ao usuário, parar o fluxo do sistema para que seja tomada alguma ação a respeito e mostra a msg, então não server apenas pegar o retorno inteiro e apresentar na tela como é feito no exemplo, par ao exemplo isso está perfeito pois executamos cada método de forma manual e individual, porém para o sistema de produção ele tem que saber ler o que ocorreu e que caminho tomar. Por isso a necessidade de ler o INI.

 

 

  • Membros Pro
Postado

Prezado, já resolvi. Obrigado. 

 

    public class IniFiles
    {
        private readonly string filePath;
        private int capacity = 512;

        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        private static extern int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder value, int size, string filePath);

        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        static extern int GetPrivateProfileString(string section, string key, string defaultValue, [In, Out] char[] value, int size, string filePath);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private static extern int GetPrivateProfileSection(string section, IntPtr keyValue, int size, string filePath);

        [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool WritePrivateProfileString(string section, string key, string value, string filePath);

        public IniFiles(string Path)
        {
            filePath = Path;
        }

        public string ReadValue(string section, string key, string defaultValue = "")
        {
            var value = new StringBuilder(capacity);
            GetPrivateProfileString(section, key, defaultValue, value, value.Capacity, filePath);
            return value.ToString();
        }

        public string[] ReadSections()
        {
            // first line will not recognize if ini file is saved in UTF-8 with BOM   
            while (true)
            {
                char[] chars = new char[capacity];
                int size = GetPrivateProfileString(null, null, "", chars, capacity, filePath);

                if (size == 0)
                {
                    return null;
                }

                if (size < capacity - 2)
                {
                    string result = new String(chars, 0, size);
                    string[] sections = result.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
                    return sections;
                }

                capacity = capacity * 2;
            }
        }

        public string[] ReadKeys(string section)
        {
            // first line will not recognize if ini file is saved in UTF-8 with BOM   
            while (true)
            {
                char[] chars = new char[capacity];
                int size = GetPrivateProfileString(section, null, "", chars, capacity, filePath);

                if (size == 0)
                {
                    return null;
                }

                if (size < capacity - 2)
                {
                    string result = new String(chars, 0, size);
                    string[] keys = result.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
                    return keys;
                }

                capacity = capacity * 2;
            }
        }

        public string[] ReadKeyValuePairs(string section)
        {
            while (true)
            {
                IntPtr returnedString = Marshal.AllocCoTaskMem(capacity * sizeof(char));
                int size = GetPrivateProfileSection(section, returnedString, capacity, filePath);

                if (size == 0)
                {
                    Marshal.FreeCoTaskMem(returnedString);
                    return null;
                }

                if (size < capacity - 2)
                {
                    string result = Marshal.PtrToStringAuto(returnedString, size - 1);
                    Marshal.FreeCoTaskMem(returnedString);
                    string[] keyValuePairs = result.Split('\0');
                    return keyValuePairs;
                }

                Marshal.FreeCoTaskMem(returnedString);
                capacity = capacity * 2;
            }
        }

        public bool WriteValue(string section, string key, string value)
        {
            bool result = WritePrivateProfileString(section, key, value, filePath);
            return result;
        }

        public bool DeleteSection(string section)
        {
            bool result = WritePrivateProfileString(section, null, null, filePath);
            return result;
        }

        public bool DeleteKey(string section, string key)
        {
            bool result = WritePrivateProfileString(section, key, null, filePath);
            return result;
        }
    }
 

 

 

Fonte:  https://www.webtips.com.br/Home/Detail/73#:~:text=NET %2F C %23%2C mas não,pela Platform Invoke (PInvoke).

 

  • Este tópico foi criado há 1605 dias atrás.
  • Talvez seja melhor você criar um NOVO TÓPICO do que postar uma resposta aqui.
Visitante
Este tópico está agora fechado para novas respostas
×
×
  • Criar Novo...

Informação Importante

Colocamos cookies em seu dispositivo para ajudar a tornar este site melhor. Você pode ajustar suas configurações de cookies, caso contrário, assumiremos que você está bem para continuar.

The popup will be closed in 10 segundos...
The popup will be closed in 10 segundos...