What is FileStream for?

1

I was reviewing my C # classes when I came across this

        SaveFileDialog f = new SaveFileDialog();
        f.Filter = "Archivo de Texto (.txt)|*.txt";
        if (f.ShowDialog() == DialogResult.OK)
        {
            StreamWriter w = new StreamWriter(f.FileName);
            w.Write(txtBlock.Text);
            w.Close();
        }

And I remembered that once my teacher used FileStream

        SaveFileDialog f = new SaveFileDialog();
        f.Filter = "Archivo de Texto (.txt)|*.txt";
        if (f.ShowDialog() == DialogResult.OK)
        {
            FileStream s = new FileStream(f.FileName, FileMode.Create, FileAccess.Write);                
            StreamWriter w = new StreamWriter(s);
            w.Write(txtBlock.Text);
            w.Close();
        }

And I would like to know what is the difference between using it or not.

PSDT: This code is used as an example to create a basic notepad

    
asked by Leonel Salcedo Retamozo 23.10.2018 в 00:27
source

1 answer

3

There are different ways to do the same, only you use a different constructor of the StreamWriter according to your data

If you have the name of the file you will pass that by parameters, I do not know if you know that you can access the code of net framework to analyze it inside

StreamWriter Resource Code

You will see how when you pass only the route this in creates a FileStream , only that the class does it for you so that you do not have to write the code if you have the route to the file

There may be other cases where you generate a document in memory so you will have a MemoryStream , in this case there is no physical path to the file, this inherits from Stream, just like FileStream

    
answered by 23.10.2018 / 01:30
source