Save as txt c #

0

I created a method that saves a txt in the user's temp folder, but would like the user to choose where to save it on the machine (save as).

How could you do this? Here is my code below:

protected void btn_txt_Click(object sender, EventArgs e)
{

    List<eFC30> Ltxt = (List<eFC30>)Session["lsttxt"];
    if (Ltxt.Count > 0)
    {

        try
        {

            DateTime dt = DateTime.Now; // Or whatever
            string d = dt.ToString("ddMMyyyyHHmmss");
            string a = (d.Substring(0, 8) + "_" + d.Substring(8, 6));
            string filename = "FC30_" + FC30_INPUT_MATRICULA.Text + "_" + a + ".txt";
            string path = @"c:\temp\FC30_" + FC30_INPUT_MATRICULA.Text + "_" + a + ".txt";

            using (StreamWriter sw = File.CreateText("Documento.txt"))
            {
                sw.WriteLine(FC30_INPUT_MATRICULA.Text + " " + FC30_NOME.Text);
                sw.WriteLine("MEDICAO" + "   " + "CONTEUDO" );

                foreach (eFC30 f in Ltxt)
                {
                    if (f.ENT_MEDANO == "")
                    {
                        sw.WriteLine("     " + " " + f.ENT_CONTEUDO );
                    }
                    else
                    {
                        sw.WriteLine(f.ENT_MEDANO + " " + f.ENT_CONTEUDO );
                    }

                    string a1 = f.ENT_MEDANO;
                    string a2 = f.ENT_CONTEUDO;
                    string a3 = f.ENT_USUARIO;
                    string a4 = f.ENT_LINHA;

                }

                sw.Close();

            }

        }
        catch (Exception ex)
        {
            //porque ele está sendo usado por outro processo.
            string err = ex.Message;
        }
    }


}
    
asked by anonymous 05.12.2018 / 17:37

1 answer

2

Ideally, you use a FolderBrowserDialog , where the user selects the location wherever the file is created:

protected void btn_txt_Click(object sender, EventArgs e)
{
    List<eFC30> Ltxt = (List<eFC30>)Session["lsttxt"];

    if (Ltxt.Count > 0)
    {
        try
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();

            if (folder.ShowDialog() == DialogResult.OK)
            {
                string path = Path.Combine(folder.SelectedPath, "Documento.txt");

                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine(FC30_INPUT_MATRICULA.Text + " " + FC30_NOME.Text);
                    sw.WriteLine("MEDICAO" + "   " + "CONTEUDO" );

                    foreach (eFC30 f in Ltxt)
                    {
                        if (f.ENT_MEDANO == "")
                            sw.WriteLine("     " + " " + f.ENT_CONTEUDO );
                        else
                            sw.WriteLine(f.ENT_MEDANO + " " + f.ENT_CONTEUDO );

                        string a1 = f.ENT_MEDANO;
                        string a2 = f.ENT_CONTEUDO;
                        string a3 = f.ENT_USUARIO;
                        string a4 = f.ENT_LINHA;
                    }

                    sw.Close();
                }
            }
        }
        catch (Exception ex)
        {
            //porque ele está sendo usado por outro processo.
            string err = ex.Message;
        }
    }
}
    
05.12.2018 / 18:05