using System; using System.IO; class Program { static void Main(string[] args) { // open a FileStream using (FileStream fs = new FileStream("data.txt", FileMode.Open)) { // get the current position long currentPosition = fs.Position; Console.WriteLine("Current Position: {0}", currentPosition); } } }
using System; using System.IO; class Program { static void Main(string[] args) { // create a MemoryStream MemoryStream ms = new MemoryStream(); // write some bytes to the stream byte[] bytes = new byte[] { 1, 2, 3, 4, 5 }; ms.Write(bytes, 0, bytes.Length); // set the position to the beginning of the stream ms.Position = 0; // read the bytes from the stream byte[] buffer = new byte[5]; ms.Read(buffer, 0, 5); // display the bytes Console.WriteLine("Bytes Read: {0}", BitConverter.ToString(buffer)); // get the current position long currentPosition = ms.Position; Console.WriteLine("Current Position: {0}", currentPosition); } }This example shows how to use GetPosition to set the position in a MemoryStream and retrieve the current position. The System GetPosition method is a member of the System.IO namespace in the .NET Framework Class Library.