MVC C # create file and save it

0

Friends I am trying to create a txt and save it in my project, but I have some problems, I did something similar a few days ago with the difference that it is already a file to my method, in this I'm just creating it but I have problems to save it, someone can help me.

    if (abono != null && cargo != null && saldo != null && refer != null)
            {

                Random rnd = new Random();
                int rndx = rnd.Next(0, 1000);
                

                var folder = Server.MapPath("~/Facturas/" + banco + "_" + rndx);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);

                       StreamWriter File = new StreamWriter("~/Facturas/" + banco + "_" + rndx + ".txt");
                File.Write(DateTime.Now + ", " + refer + ", " + cargo + ", " + abono + ", " + saldo);

                using (StreamWriter writer = File.CreateText(folder))
                {
                    writer.WriteLineAsync("First line of example");
                    writer.WriteLineAsync("and second line");
                }

                File.Close();
                }



            }

Here my mistake

    
asked by E.Rawrdríguez.Ophanim 19.05.2018 в 01:09
source

1 answer

1

What you need is to instantiate your StreamWriter in the following way (class TextWriter is abstract so you can not instantiate it directly.

    if (abono != null && cargo != null && saldo != null && refer != null)
            {

                Random rnd = new Random();
                int rndx = rnd.Next(0, 1000);


                var folder = Server.MapPath("~/Facturas/" + banco + "_" + rndx);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);

                     string filePath = System.IO.Path.Combine(folder, "fichero.txt") ;

                using (StreamWriter writer = File.CreateText(filePath))
                {
                    writer.WriteLine(DateTime.Now + ", " + refer + ", " + cargo + ", " + abono + ", " + saldo");

                }
}
    
answered by 19.05.2018 / 01:30
source