Save as txt c#

Asked

Viewed 174 times

0

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

How could I do that? Below is my code:

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;
        }
    }


}

1 answer

2


The ideal is to 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;
        }
    }
}
  • It worked!! thank you very much!!

Browser other questions tagged

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