I recently read on Setting position index of filestream that it is possible to use
seek() method of BaseStream class.
But now, using .Net Framework 4.8 or greater these 2 functions seem to have been deleted.
Which stream in .Net 4.8 or greater does implement this 2 functions ?
I search a solution that is distinct from My.MyComputer.SystemFile.Seek() and is not restricted to using old VB 6 FileOpen() method !
The
System.IO.FileStreamclass inherits theSystem.IO.Streamclass. The latter provides the base functionality for all streams while the former provides functionality for streams backed by files. TheSeekmethod is a member of theStreamclass and thus every stream, regardless of type, has that method. That's true up to .NET 6 and will continue to be true as long as .NET exists.The
Seekmethod might throw aNotSupportedExceptionin some cases, in which case theCanSeekproperty of that stream will beFalse. If you have a stream and you're not sure whether it can seek or not, test that property before callingSeekto ensure no exception will be thrown. In the case of aFileStream, the documentation (which you should already have read) tells us when to expect that property to beFalse:You can read the documentation of other types of streams to see whether they support seeking, e.g.
MemoryStreamdoes andNetworkStreamdoesn't. Basically, seeking requires random access to all the data rather than just sequential access.There is no
BaseStreamproperty on aFileStream. The property you refer to is a member ofStreamReaderclass. That property will return aStreamreference to the stream being read. That could be aFileStream,NetworkStream,MemoryStreamor whatever. If you have aStreamReaderand you want to seek to a specific position in the underlying stream but you don't know whether it is supported or not then you get theBaseStream, test theCanSeekproperty and, if it'sTrue, call theSeekmethod.That will work no matter what type of stream backs the reader because
CanSeekandSeekare members ofStreamandStreamReader.BaseStreamis typeStream.