Home > Back-end >  How to choose where append data?
How to choose where append data?

Time:03-01

I'm a begginer programmer and I'm learning C#, I have a big difficulty with Files. Someone can help me with this, please?

I created a .txt file and I want to append text choosing the file location instead creating/saving in the startup path just like my code does.

This is what I have:

public void btnGuardar_Click(object sender, EventArgs e)
    {
        string caminho = Application.StartupPath   "ID_Cartões.txt";

        if (lblDEC.Text != "")
        {
            StreamWriter txt = File.AppendText(caminho);
            txt.WriteLine("ID: "   lblDEC.Text   "\r\n"   DateTime.UtcNow.ToString("dd/MM/yyyy HH:mm:ss")   "\r\n");
            txt.Close();

            MessageBox.Show("Ficheiro guardado com sucesso!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else
        {
            MessageBox.Show("Hexadecimal não convertido.", "ERRO!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

CodePudding user response:

Winforms has a SaveFileDialog control where the user can choose the location and file name or a FolderBrowserDialog control where they can choose a folder and you specify the filename.

CodePudding user response:

You can use an OpenFileDialog, the FileName property contains the file path of the file selected by the user.

OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK) 
{ 
  string filePath = openFileDialog.FileName; 
} 
  • Related