Save a txt in my solution

1

Hello, I hope you can help me.

I have this code

using(StreamWriter escritor = new StreamWriter(@"C:\Prueba.txt"))
{
    escritor.WriteLine("Hola");
}

The problem is that when I upload it to my host it does not save the file by the route and I would like the file to be saved inside my solution and not in "C"

    
asked by LuisMtz 03.08.2018 в 17:56
source

4 answers

0

Here is the solution

using(StreamWriter escritor = new StreamWriter(Server.MapPath("~/Prueba.txt"), true))
{
       escritor.WriteLine("Hola");
}
    
answered by 03.08.2018 / 18:24
source
0

You should use the Server class

Server.MapPath("Nombre de la carpeta dentro de tu proyecto")+ "\" + NombreArchivo

With this he goes and searches at the project level.

    
answered by 03.08.2018 в 18:01
0

Try this way to see how:

using (StreamWriter escritor = new StreamWriter(@"..\..\Prueba.txt"))
{
    escritor.WriteLine("Hola");
}
    
answered by 03.08.2018 в 18:06
0

You can use a path relative to the root of your project as explained in the Microsoft documentation

  

You can use the ~ operator together with folders to specify a   path that is based on the current root.

using(StreamWriter escritor = new StreamWriter("~\Prueba.txt"))
{
    escritor.WriteLine("Hola");
}
    
answered by 03.08.2018 в 18:04