Generate text file from a console application

Asked

Viewed 804 times

2

I need to adapt this method of an application Windows Forms https://github.com/BoletoNet/boletonet for Console Application, that is, instead of using the object saveFileDialog.ShowDialog() I have to create the file using another class method Stream:

Method Used in Windows Forms application:

public void GeraArquivoCNAB400(IBanco banco, Cedente cedente, Boletos boletos)
{
    try
    {
        saveFileDialog.Filter = "Arquivos de Retorno (*.rem)|*.rem|Todos Arquivos (*.*)|*.*";
        if (saveFileDialog.ShowDialog() == DialogResult.OK)
        {
            ArquivoRemessa arquivo = new ArquivoRemessa(TipoArquivo.CNAB400);

            //Valida a Remessa Correspondentes antes de Gerar a mesma...
            string vMsgRetorno = string.Empty;
            bool vValouOK = arquivo.ValidarArquivoRemessa(cedente.Convenio.ToString(), banco, cedente, boletos, 1, out vMsgRetorno);
            if (!vValouOK)
            {
                MessageBox.Show(String.Concat("Foram localizados inconsistências na validação da remessa!", Environment.NewLine, vMsgRetorno),
                                "Teste",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
            else
            {
                arquivo.GerarArquivoRemessa("0", banco, cedente, boletos, saveFileDialog.OpenFile(), 1);

                MessageBox.Show("Arquivo gerado com sucesso!", "Teste",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

I need to adapt to be used in a Console Application application.

  • What exactly do you need? It seems to me to just take those parts that are from Windows Forms and replace with a fixed value.

  • How would I replace it with a fixed amount ?

2 answers

3

Take out all parts of Windows Forms. The name of the file that is coming from saveFileDialog.OpenFile() will be replaced in general by (for example):

new FileStream("caminho do arquivo aqui", FileAccess.Write);

Documentation. May vary as needed.

Other ways may be more suitable depending on the situation.

  • Very well it worked.

3


Just take out the dependencies of System.Windows.Forms. SaveFileDialog and MessageBox are two things you will not be able to use, as they are part of the namespace System.Windos.Forms.

How the file path is coming from SaveFileDialog, you will have to pass the path "manually" or develop some function that enables the user to choose this path (I have not left this way because it is not specified in the question).

I posted the messages of MessageBox on the console.

public void GeraArquivoCNAB400(IBanco banco, Cedente cedente, Boletos boletos)
{    
    ArquivoRemessa arquivo = new ArquivoRemessa(TipoArquivo.CNAB400);

    //Valida a Remessa Correspondentes antes de Gerar a mesma...
    string vMsgRetorno = string.Empty;
    bool vValouOK = arquivo.ValidarArquivoRemessa(cedente.Convenio.ToString(), banco, cedente, boletos, 1, out vMsgRetorno);

    if (!vValouOK)
    {
        Console.WriteLine(String.Concat("Foram localizados inconsistências na validação da remessa!", Environment.NewLine, vMsgRetorno));
    }
    else
    {
        arquivo.GerarArquivoRemessa("0", banco, cedente, boletos, new FileStream(@"C:\PastaQualquer\Arquivo.txt", FileAccess.Write), 1);
        //No lugar de C:\PastaQualquer\Arquivo.txt você coloca o caminho do arquivo

        Console.WriteLine("Arquivo gerado com sucesso!");
    }
}
  • Perfect, it worked thanks!

Browser other questions tagged

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