C# Miscellaneous Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
To convert a Stream
into a byte[]
in C#, you can use the MemoryStream
class to read the contents of the Stream
and copy them into a new byte[]
.
Here is an example:
public byte[] ReadFully(Stream input) { using (MemoryStream ms = new MemoryStream()) { input.CopyTo(ms); return ms.ToArray(); } }
In this example, the ReadFully
method takes a Stream
as input and creates a new MemoryStream
to read the contents of the Stream
. The Stream.CopyTo
method is used to copy the contents of the input Stream
to the MemoryStream
, and then the MemoryStream.ToArray
method is used to convert the contents of the MemoryStream
into a byte[]
.
To convert a byte[]
into a Stream
in C#, you can use the MemoryStream
class to create a new Stream
and write the contents of the byte[]
to the new Stream
.
Here is an example:
public Stream BytesToStream(byte[] bytes) { MemoryStream stream = new MemoryStream(); stream.Write(bytes, 0, bytes.Length); stream.Position = 0; return stream; }
In this example, the BytesToStream
method takes a byte[]
as input and creates a new MemoryStream
to write the contents of the byte[]
to. The MemoryStream.Write
method is used to write the contents of the byte[]
to the MemoryStream
, and then the MemoryStream.Position
property is set to 0 to ensure that the Stream
is reset to the beginning. Finally, the method returns the new MemoryStream
as a Stream
.
To convert a string
to a Stream
in C#, you can use the MemoryStream
class to create a new Stream
and write the contents of the string
to the new Stream
using a StreamWriter
.
Here is an example:
public Stream StringToStream(string s) { MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); writer.Write(s); writer.Flush(); stream.Position = 0; return stream; }
In this example, the StringToStream
method takes a string
as input and creates a new MemoryStream
to write the contents of the string
to. The StreamWriter
is used to write the contents of the string
to the MemoryStream
, and then the StreamWriter.Flush
method is used to ensure that all data is written to the MemoryStream
. Finally, the MemoryStream.Position
property is set to 0 to ensure that the Stream
is reset to the beginning. Finally, the method returns the new MemoryStream
as a Stream
.
To create a temporary file from a stream in C#, you can use the Path.GetTempFileName()
method to generate a unique file name in the temporary folder, and then write the stream contents to that file using a FileStream
. For example:
string tempFilePath = Path.GetTempFileName(); using (var fileStream = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write)) { stream.CopyTo(fileStream); }
To save an XmlDocument
to a MemoryStream
in C#, you can use the XmlDocument.Save()
method with a MemoryStream
as the XmlWriter
. For example:
XmlDocument doc = new XmlDocument(); // Load or create the document // ... // Save the document to a MemoryStream using (MemoryStream stream = new MemoryStream()) { XmlWriterSettings settings = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8 }; using (XmlWriter writer = XmlWriter.Create(stream, settings)) { doc.Save(writer); } // Do something with the MemoryStream }
To copy the contents of one Stream
to another Stream
in C#, you can use the Stream.CopyTo()
method. For example:
using (Stream sourceStream = GetSourceStream()) using (Stream targetStream = GetTargetStream()) { sourceStream.CopyTo(targetStream); }
Note that GetSourceStream()
and GetTargetStream()
should be replaced with the actual methods that return the source and target streams.
C# read from stream:
using (Stream stream = new FileStream("example.txt", FileMode.Open)) { byte[] buffer = new byte[1024]; int bytesRead = stream.Read(buffer, 0, buffer.Length); // Process the read data }
Write to stream in C#:
using (Stream stream = new FileStream("example.txt", FileMode.Create)) { byte[] data = Encoding.UTF8.GetBytes("Hello, Stream!"); stream.Write(data, 0, data.Length); }
C# MemoryStream example:
using (MemoryStream memoryStream = new MemoryStream()) { // Read/write operations on the memory stream }
File stream in C#:
using (FileStream fileStream = new FileStream("example.txt", FileMode.Open)) { // Read/write operations on the file stream }
C# FileStream read and write:
using (FileStream fileStream = new FileStream("example.txt", FileMode.OpenOrCreate)) { byte[] buffer = new byte[1024]; int bytesRead = fileStream.Read(buffer, 0, buffer.Length); // Process the read data // Write data byte[] data = Encoding.UTF8.GetBytes("Hello, FileStream!"); fileStream.Write(data, 0, data.Length); }
Using StreamReader in C#:
using (StreamReader reader = new StreamReader("example.txt")) { string line = reader.ReadLine(); // Process the read line }
C# StreamWriter example:
using (StreamWriter writer = new StreamWriter("example.txt")) { writer.WriteLine("Hello, StreamWriter!"); }
BufferedStream in C#:
using (FileStream fileStream = new FileStream("example.txt", FileMode.Open)) using (BufferedStream bufferedStream = new BufferedStream(fileStream)) { // Read/write operations on the buffered stream }
C# FileStream create and write:
using (FileStream fileStream = new FileStream("example.txt", FileMode.Create)) { byte[] data = Encoding.UTF8.GetBytes("Hello, FileStream!"); fileStream.Write(data, 0, data.Length); }
Using MemoryStream in C#:
using (MemoryStream memoryStream = new MemoryStream()) { // Read/write operations on the memory stream }
C# Stream.CopyTo method:
using (Stream sourceStream = new FileStream("source.txt", FileMode.Open)) using (Stream destinationStream = new FileStream("destination.txt", FileMode.Create)) { sourceStream.CopyTo(destinationStream); }
Read file line by line using StreamReader in C#:
using (StreamReader reader = new StreamReader("example.txt")) { while (!reader.EndOfStream) { string line = reader.ReadLine(); // Process the read line } }
C# Stream.Seek method:
using (Stream stream = new FileStream("example.txt", FileMode.Open)) { stream.Seek(10, SeekOrigin.Begin); // Perform operations after seeking }
Asynchronous stream operations in C#:
using (FileStream fileStream = new FileStream("example.txt", FileMode.Open)) { byte[] buffer = new byte[1024]; int bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length); // Process the read data asynchronously }
C# FileStream read and write binary data:
using (FileStream fileStream = new FileStream("example.dat", FileMode.OpenOrCreate)) { BinaryWriter writer = new BinaryWriter(fileStream); writer.Write(42); writer.Write("Hello, BinaryWriter!"); writer.Close(); BinaryReader reader = new BinaryReader(fileStream); int intValue = reader.ReadInt32(); string stringValue = reader.ReadString(); reader.Close(); }
MemoryStream to byte array in C#:
byte[] byteArray; using (MemoryStream memoryStream = new MemoryStream()) { // Write data to the memory stream byteArray = memoryStream.ToArray(); }
C# Stream.CopyToAsync method:
using (Stream sourceStream = new FileStream("source.txt", FileMode.Open)) using (Stream destinationStream = new FileStream("destination.txt", FileMode.Create)) { await sourceStream.CopyToAsync(destinationStream); }
Working with network streams in C#:
TcpClient client = new TcpClient("localhost", 8080); using (NetworkStream networkStream = client.GetStream()) { // Read/write operations on the network stream }