How can I make a program open a saved file on the computer without this program running?

Asked

Viewed 82 times

1

Hello! I would like to share a question that I am having in a program that I am developing of the Windows Application type in C#. This is a notepad for my personal use.

The operation of this application is the same as the standard notepad, that is, the user types a text and can save a file with this text. To avoid confusion with other text files, the program saves an extension defined as *.tst . In addition to the save and save as button, there is the open button, which opens a dialog box for the user to choose a file. tst to be shown or edited in the program itself.

However, I cannot implement for that user by clicking on a file. tst saved on your pc, run the program already with that file information loaded in the text box (as with the files in other extensions that can be opened with your default applications).

The System.IO.Directory.Getcurrentdirectory() method returns the directory path of the file that ran the program. I believe it can be used so that the application can realize what has been said.

Below, I present the code used by the application. Also, I attached an image of the application form.

inserir a descrição da imagem aqui

using System; using System.IO; using System.Text; using System.Windows.Forms;

namespace Blocodenotes { public partial class frmBNote : Form { string pathAbrir, pathSalvar; //string with the file path to save and the file to open string pathSalvarCopy; //saves a copy of the path to save the file

    public frmBNote()
    {
        InitializeComponent();
    }

    private void frmBNote_Load(object sender, EventArgs e)
    {
        string caminhoArq = Directory.GetCurrentDirectory(); //caminho do arquivo (.tst ou .exe) que executou a aplicação
        string caminhoExe = Application.StartupPath; //caminho do executável da aplicação
    }

    void Salvar() //método para salvar o texto inserido pelo usuário na TextBox
    {
        SaveFileDialog caixaSalvar = new SaveFileDialog();

        caixaSalvar.Filter = "(*.tst) | *.tst"; //extensão dos arquivos de texto criados no programa
        caixaSalvar.DefaultExt = "tst"; //definição dessa extensão

        if (string.IsNullOrWhiteSpace(caminhoSalvar))
        {
            caixaSalvar.ShowDialog();
            caminhoSalvar = caixaSalvar.FileName;
            caixaSalvar.Dispose();

            try
            {
                File.WriteAllText(caminhoSalvar, TxtJanela.Text, Encoding.Default);
                this.Text = "B-NOTES - " + Path.GetFileNameWithoutExtension(caminhoSalvar);
                MessageBox.Show("Arquivo salvo!");
            }
            catch
            {
                caminhoSalvar = caminhoSalvarCopia;
                MessageBox.Show("O arquivo não foi salvo.", "ATENÇÃO!");
                return;
            }
        }
        else
        {
            File.WriteAllText(caminhoSalvar, TxtJanela.Text, Encoding.Default);

            MessageBox.Show("Salvo!");
        }
    }

    private void SalvarClick(object sender, EventArgs e)
    {
        Salvar();
    }

    private void salvarComoToolStripMenuItem_Click(object sender, EventArgs e)
    {
        caminhoSalvarCopia = caminhoSalvar;
        caminhoSalvar = null;

        Salvar();
    }

    void Abrir() //método para abrir na TextBox o texto salvo no arquivo escolhido pelo usuário
    {
        OpenFileDialog caixaAbrir = new OpenFileDialog();

        caixaAbrir.Filter = "(*.tst) | *.tst";
        caixaAbrir.DefaultExt = "tst";

        caixaAbrir.ShowDialog();
        caminhoAbrir = caixaAbrir.FileName;
        caixaAbrir.Dispose();

        try
        {
            TxtJanela.Text = File.ReadAllText(caminhoAbrir, Encoding.Default);
            this.Text = "B-NOTES - " + Path.GetFileNameWithoutExtension(caminhoAbrir);
            caminhoSalvar = caminhoAbrir;
        }
        catch
        {
            return;
        }
    }

    private void abrirToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Deseja salvar o arquivo atual?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            Salvar();
            Abrir();
        }
        else
        {
            Abrir();
        }
    }

    private void novoToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Deseja salvar o arquivo atual?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            Salvar();
            TxtJanela.Text = "";
            this.Text = "B-NOTES";
            caminhoAbrir = caminhoSalvar = caminhoSalvarCopia = null;
        }
        else
        {
            TxtJanela.Text = "";
            this.Text = "B-NOTES";
            caminhoAbrir = caminhoSalvar = caminhoSalvarCopia = null;
        }
    }

    private void frmBNote_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (MessageBox.Show("Deseja salvar o arquivo atual?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            Salvar();
        }
    }

    private void sairToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Close();
    }
}

}

  • I think this will help you: https://stackoverflow.com/questions/3924753/where-does-windows-store-its-open-with-settings

  • I found another similar: https://stackoverflow.com/questions/3788429/c-sharp-windows-open-with-context-menu-behaviour

  • You have to create the association of the extension with your program (add registry entries), The Operating System will call your program with the file as argument, In your Main will come a parameter that is the file to be opened. In a basic search you find all this on google. Another point, if you want to open always in the same window you will have to implement a logic of SINGLETON (Single Instance or single instance), and send a message to the program already opened with the file path.

  • But if you don’t need to distribute this to customers is easier, right-click on the file, select open with... click search and find your program, if not open at first is why your program does not handle the argument sent by the operating system in Main(string[] args) <= args contains the file to open. You can select "Always use this app..." to create the permanent membership.

  • Thank you very much, guys! I got it here. I modified the Main() method for Main(string[] args) and it worked super well with some adjustments.

No answers

Browser other questions tagged

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