Automatically save to Default Folder C#

Asked

Viewed 1,118 times

2

Can someone tell me the C# method for saving a document to a default folder in Code? In this case wanted the program to automatically save itself to the Desktop.

This is the code I currently have on the button, but I intend to do it right on Load.

 private void button1_Click(object sender, EventArgs e)
    {
      SaveFileDialog salvar = new SaveFileDialog();
        salvar.Filter = "Ficheiro de Configuração|*.cnf";
        salvar.DefaultExt = "txt";
        DialogResult salvou = salvar.ShowDialog();
        if (salvou == DialogResult.OK)
        {
            StreamWriter sw = null;
            try
            {
                sw = new StreamWriter(salvar.FileName);
                sw.WriteLine(txtConfig.Text);
                MessageBox.Show("Gravado com Sucesso!", "Sucesso",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
            }
            catch (IOException ex)
            {
                MessageBox.Show("IOException:\r\n\r\n" + ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception : \r\n\r\n" + ex.Message);
            }
            finally
            {
                if (sw != null)
                    sw.Close();
            }
            this.Close();
        }
 }

Cumps

  • What kind of document? What do you mean 'automatically'?

  • @williamhk2 I have a textbox with written values. I currently have a button that when clicking, opens the Savedialog, now I wanted it to be automatically in Load, that is, when opening the Form, whatever is inside the textbox will save the file . txt on Desktop. Cumps.

  • Could you post the code snippet? and describe what you commented, in the question?

  • 1

    Already a friend @williamhk2.

  • Beauty ;) @Godfathersantana

1 answer

1


In its function associated with the event FormLoad:

private void YourForm_Load(object sender, EventArgs e)
{
    var folder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    var fileName = Path.Combine(folder, "NomeFixoDoArquivo.cnf");

    File.WriteAllText(fileName, txtConfig.Text);
}

Environment.SpecialFolder.Desktop is one of the ways to recover the logged user’s desktop folder.

File.WriteAllText(fileName, txtConfig.Text) allows you to write all the contents of the file at once.

  • Perfect friend @Jonnypiazzi! And you can open a folder on the Desktop and put that file inside it?

  • And by the way, then in the other form, I want the program to open the file you saved, that is, the "Filename.cnf".

  • the variable fileName has the file name. You can run this same code in the other form and open the file.

  • Moreover the class File has a method File.ReadAllText which reads all the contents of a file and returns a string. For more details please ask another question. And don’t forget to mark it as the correct answer if it helped you.

  • Amigo @Jonnypiazzi how do I get the other form to open with an Openfiledialog the file you saved? She has to automatically retrieve the file name that was saved.

Browser other questions tagged

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