How to open a . txt file and play text for my Textbox in Winforms?

Asked

Viewed 75 times

1

Hello, everybody!

For study purposes, I created my own custom notepad, it can open files and get the text of a common Windows notepad through Openfiledialog and transfer to my program.

inserir a descrição da imagem aqui

However I want that when the user opens the file from the windows interface, the text that file is transferred to my current one, but I don’t know how to do it without using Openfiledialog.

inserir a descrição da imagem aqui

This method happens when the user clicks on "Open File". The abrirArquivo.FileName provides me the path and from that path I can transfer data to my program. I want to do the same thing when the user clicks on "Open" from Windows, but without using Openfiledialog.

private void MenuAbrirArquivo_Click(object sender, EventArgs e)
        {
            // Cria uma janela para abrir um arquivo
            var abrirArquivo = new OpenFileDialog();
            abrirArquivo.Title = "Selecione um arquivo";
            abrirArquivo.Filter = "txt file|*txt";
            abrirArquivo.RestoreDirectory = true;

            // Pega o resultado de qual botão ele clicou
            var resultado = abrirArquivo.ShowDialog();

            // User escolheu um arquivo
            if (resultado == DialogResult.OK)
            {
                // Cria um leitor para transferir os dados para o programa atual
                using(var leitor = new StreamReader(abrirArquivo.FileName))
                {
                    txtTexto.Text = leitor.ReadToEnd();
                }
                nomeArquivo = abrirArquivo.SafeFileName;
                path = abrirArquivo.FileName;
                SalvarModificacoes();
            }
        }

1 answer

2

You have to configure windows to always open txt files using your application or use "Open With" option and select your executable. Once done, your application has to know how to receive the information from the file that was sent to be opened, this can be done by rescuing the 'arguments' passed to the application at the time of triggering it:

string[] args = Environment.GetCommandLineArgs();

Probably args[1] will bring the file path, from there you load the file as you would if it opened by the Openfiledlg context.

Doing this in a form event where it runs as soon as it loads would be the ideal point and obviously you need to check if it returned any argument, because when the user simply opens the application, it will have no arguments at all.

string[] args = Environment.GetCommandLineArgs();

if (args.Lenght > 1)
{
    AbrirArquivo(args[1]); // chamar seu método para abrir o arquivo a partir do caminho dele.
}
  • Thank you very much, Leandro! I spent 3 days looking for how to get this information and you helped me a lot! I thank you from my heart =D. But the way of the . txt is not in args[0], it is in args[1].

  • You are right, I was in doubt of that point when I answered, the args[0] gets the name of the exe if I am not mistaken. Updated answer!

Browser other questions tagged

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