public Seek ( int offset, SeekOrigin origin ) : long | ||
offset | int | |
origin | SeekOrigin | |
return | long |
using (BinaryWriter writer = new BinaryWriter(File.Open("data.bin", FileMode.Create))) { writer.Write(42); writer.Write("Hello World!"); // Seek to the beginning of the file and overwrite the first value writer.Seek(0, SeekOrigin.Begin); writer.Write(100); }
using (BinaryWriter writer = new BinaryWriter(File.Open("data.bin", FileMode.Create))) { writer.Write(42); writer.Write("Hello World!"); // Seek to the middle of the file and insert a new value writer.Seek(4, SeekOrigin.Begin); writer.Write("Inserted Value"); }In this example, we create a binary file and write two values to it. Then, we use the Seek function to move the write position to the middle of the file and insert a new value. This functionality is part of the System.IO namespace in the .NET Framework, which is included in the .NET Standard library. Therefore, no additional package library is needed.