public Seek ( long offset, SeekOrigin origin ) : long | ||
offset | long | |
origin | SeekOrigin | |
return | long |
using (var fileStream = new FileStream("example.txt", FileMode.Open)) { using (var bufferedStream = new BufferedStream(fileStream)) { // Move the position of the buffer to the 10th byte of the stream. bufferedStream.Seek(10, SeekOrigin.Begin); // Read the next 5 bytes from the buffer. var buffer = new byte[5]; bufferedStream.Read(buffer, 0, 5); } }
using (var fileStream = new FileStream("example.txt", FileMode.Open)) { using (var bufferedStream = new BufferedStream(fileStream)) { // Move the position of the buffer to the end of the stream. bufferedStream.Seek(0, SeekOrigin.End); // Write a new line to the end of the file. using (var writer = new StreamWriter(bufferedStream)) { writer.WriteLine("This is a new line."); } } }In this example, we use the Seek method to move the position of the buffer to the end of the stream. Then, we use a StreamWriter to write a new line to the end of the file.