According to MSDN documentation for FileStream.SafeFileHandle :
The SafeFileHandle property automatically clears the stream and sets the current position of the stream to 0. This allows you to move the file or stream position reset to another stream using the SafeFileHandle returned by this property.
However, my tests show that the position of the stream does not change.
Consider the following code:
using System; using System.IO; namespace Demo { internal static class Program { public static void Main() { Directory.CreateDirectory("C:\\TEST"); var buffer = new byte[1024]; using (var file = new FileStream("C:\\TEST\\TEST.BIN", FileMode.Create)) { file.Write(buffer, 0, buffer.Length); Console.WriteLine(file.Position);
If accessing SafeFileHandle really did reset the current position of the stream to 0, I would expect the second WriteLine () to print 0.
I have other tests in which I actually use SafeFileHandle with the Windows API ReadFile () and WriteFile () methods, and even then it will not change the file pointer.
I have a code that uses SafeFileHandle , so it is very important for me whether the position of the stream will change or not!
Am I misunderstood the documentation, or is it wrong? Or sometimes does it change the position of the flow? (That would be a nightmare!)
source share