C# FileStream Example, File.CreateThis C# example page uses FileStream and File.Create to create a file. Another object like StreamWriter can be used with FileStream.
In the .NET Framework we often access files through streams. We can place one stream on top of another (usually with "using" statements).File
This is powerful. For the FileStream, we often need to use another stream (like StreamWriter) on top of it. We can do many things with a file using a FileStream.
Here we get a FileStream using the File.Create method. Other methods, like File.Open or File.OpenText can be used to get a FileStream.File.Open
StreamWriter: We pass the FileStream object we get from File.Create to a StreamWriter. We then use WriteLine to write to the file.StreamWriter
C# program that uses FileStream, File.Create
static void Main()
// Use FileStream with File.Create.
// ... Pass FileStream to a StreamWriter and write data to it.
using (FileStream fileStream =
using (StreamWriter writer = new StreamWriter(fileStream))
writer.WriteLine("Example 1 written");
Contents of example1.txt:
Example 1 written
It is important to understand how streams can be used together to develop complex (and custom) file handling routines in C#. Many stream types are available.
And: Often the FileStream serves as a "base" to other streams we might use. The example above shows this.
A brief summary. Understanding the dynamics of streams is important. For more complex tasks, a FileStream is often useful as many streams are combined in methods, one acting upon another.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to email@example.com.