error in can not convert from string to System.IO.Stream

1

Create a series of methods in a class to read and write text files, and in one of the methods I get an error:

public static void CreateAllText(string path, string contenidos, Encoding encoding)
    {
        if (!File.Exists(path))
        {
            using (StreamWriter sDocwriter = new StreamWriter(path, encoding, 1, false))
            {
                sDocwriter.Write(contenidos);
            }
        }
        else
        {
            throw new ArgumentNullException("Error: Existe Archivo en la Ruta Especificada");
        }
    }

on the line:

  

using (StreamWriter sDocwriter = new StreamWriter ( path , encoding, 1, false))

Just in the path at design time is that the mentioned error comes out.

    
asked by ger 06.12.2018 в 22:17
source

3 answers

1

As far as I can see these are passing the arguments of the constructor incorrectly.

The correct thing would be:

using (StreamWriter sDocwriter = new StreamWriter(path, false, encoding, 1))
{
    sDocwriter.Write(contenidos);
}
    
answered by 06.12.2018 / 22:39
source
0

The constructor what do you want to use is this:

StreamWriter(String, Boolean, Encoding, Int32)

Then you have to change your values, to match, change the encoding of place:

using (StreamWriter sDocwriter = new StreamWriter(path, 1, encoding, false))
      {
            sDocwriter.Write(contenidos);
      }
    
answered by 06.12.2018 в 22:41
-1

The constructor you are using is not the correct one:

See the Microsoft documentation on builders StreamWriter

Change it to:

new StreamWriter(path, true, encoding, 1)
    
answered by 06.12.2018 в 22:28