The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
BinaryReader Class
.NET Framework 3.0
Reads primitive data types as binary values in a specific encoding.
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
'Declaration <ComVisibleAttribute(True)> _ Public Class BinaryReader Implements IDisposable 'Usage Dim instance As BinaryReader
/** @attribute ComVisibleAttribute(true) */ public class BinaryReader implements IDisposable
ComVisibleAttribute(true) public class BinaryReader implements IDisposable
Not applicable.
For a list of common I/O tasks, see Common I/O Tasks.
The following code example demonstrates how to store and retrieve application settings in a file.
Imports Microsoft.VisualBasic Imports System Imports System.IO Imports System.Security.Permissions Public Class Test Shared Sub Main() ' Load application settings. Dim appSettings As New AppSettings() Console.WriteLine("App settings." & vbcrLf & "Aspect " & _ "Ratio: {0}, Lookup directory: {1}," & vbcrLf & "Auto " & _ "save time: {2} minutes, Show status bar: {3}" & vbCrLf, _ New Object(3){appSettings.AspectRatio.ToString(), _ appSettings.LookupDir, _ appSettings.AutoSaveTime.ToString(), _ appSettings.ShowStatusBar.ToString()}) ' Change the settings. appSettings.AspectRatio = 1.250 appSettings.LookupDir = "C:\Temp" appSettings.AutoSaveTime = 10 appSettings.ShowStatusBar = True ' Save the new settings. appSettings.Close() End Sub End Class ' Store and retrieve application settings. Public Class AppSettings Const fileName As String = "AppSettings#@@#.dat" Dim aspRatio As Single Dim lkupDir As String Dim saveTime As Integer Dim statusBar As Boolean Property AspectRatio As Single Get Return aspRatio End Get Set aspRatio = Value End Set End Property Property LookupDir As String Get Return lkupDir End Get Set lkupDir = Value End Set End Property Property AutoSaveTime As Integer Get Return saveTime End Get Set saveTime = Value End Set End Property Property ShowStatusBar As Boolean Get Return statusBar End Get Set statusBar = Value End Set End Property Sub New() ' Create default application settings. aspRatio = 1.3333 lkupDir = "C:\AppDirectory" saveTime = 30 statusBar = False If File.Exists(fileName) Then Dim binReader As New BinaryReader( _ File.Open(fileName, FileMode.Open)) Try ' If the file is not empty, ' read the application settings. ' Read 4 bytes into a buffer to ' determine if the file is empty. Dim testArray As Byte() = {0,0,0,0} Dim count As Integer = binReader.Read(testArray, 0, 3) If count <> 0 Then aspRatio = binReader.ReadSingle() lkupDir = binReader.ReadString() saveTime = binReader.ReadInt32() statusBar = binReader.ReadBoolean() Return End If ' If the end of the stream is reached before reading ' the four data values, ignore the error and use the ' default settings for the remaining values. Catch ex As EndOfStreamException Console.WriteLine("{0} caught and ignored. " & _ "Using default values.", ex.GetType().Name) Finally binReader.Close() End Try End If End Sub ' Create a file and store the application settings. Sub Close() Dim binWriter As New BinaryWriter( _ File.Open(fileName, FileMode.Create)) Try binWriter.Write(aspRatio) binWriter.Write(lkupDir) binWriter.Write(saveTime) binWriter.Write(statusBar) Finally binWriter.Close() End Try End Sub End Class
import System.*;
import System.IO.*;
import System.Security.Permissions.*;
class Test
{
public static void main(String[] args)
{
// Load application settings.
AppSettings appSettings = new AppSettings();
Console.WriteLine("App settings.\nAspect Ratio: {0}, "
+ "Lookup directory: {1},\nAuto save time: {2} minutes, "
+ "Show status bar: {3}\n",
new Object[]{System.Convert.ToString(appSettings.get_AspectRatio()),
appSettings.get_LookupDir(),
System.Convert.ToString(appSettings.get_AutoSaveTime()),
System.Convert.ToString(appSettings.get_ShowStatusBar())});
// Change the settings.
appSettings.set_AspectRatio(1.25F);
appSettings.set_LookupDir("C:\\Temp");
appSettings.set_AutoSaveTime ( 10);
appSettings.set_ShowStatusBar( true);
// Save the new settings.
appSettings.Close();
} //main
} //Test
// Store and retrieve application settings.
class AppSettings
{
private String fileName = "AppSettings#@@#.dat";
private float aspectRatio;
private String lookupDir;
private int autoSaveTime;
private boolean showStatusBar;
/** @property
*/
public float get_AspectRatio()
{
return aspectRatio ;
}//get_AspectRatio
/** @property
*/
public void set_AspectRatio ( float value )
{
aspectRatio = value;
}//set_AspectRatio
/** @property
*/
public String get_LookupDir()
{
return lookupDir ;
}//get_LookupDir
/** @property
*/
public void set_LookupDir ( String value )
{
lookupDir = value;
}//set_LookupDir
/** @property
*/
public int get_AutoSaveTime()
{
return autoSaveTime ;
}//get_AutoSaveTime
/** @property
*/
public void set_AutoSaveTime ( int value )
{
autoSaveTime = value;
}//set_AutoSaveTime
/** @property
*/
public boolean get_ShowStatusBar()
{
return showStatusBar ;
}//get_ShowStatusBar
/** @property
*/
public void set_ShowStatusBar ( boolean value )
{
showStatusBar = value;
}//set_ShowStatusBar
public AppSettings()
{
// Create default application settings.
aspectRatio = 1.3333F;
lookupDir = "C:\\AppDirectory";
autoSaveTime = 30;
showStatusBar = false;
if ( File.Exists(fileName) ) {
BinaryReader binReader =
new BinaryReader(File.Open(fileName, FileMode.Open));
try {
// If the file is not empty,
// read the application settings.
// Read 4 bytes into a buffer to
// determine if the file is empty.
ubyte[] testArray = new ubyte[3];
int count = binReader.Read(testArray, 0, 3);
if (count != 0)
{
aspectRatio = binReader.ReadSingle();
lookupDir = binReader.ReadString();
autoSaveTime = binReader.ReadInt32();
showStatusBar = binReader.ReadBoolean();
}
}
// If the end of the stream is reached before reading
// the four data values, ignore the error and use the
// default settings for the remaining values.
catch(EndOfStreamException e) {
Console.WriteLine("{0} caught and ignored. "
+ "Using default values.", e.GetType().get_Name());
}
finally {
binReader.Close();
}
}
} //AppSettings
// Create a file and store the application settings.
public void Close()
{
BinaryWriter binWriter =
new BinaryWriter(File.Open(fileName, FileMode.Create));
try {
binWriter.Write(aspectRatio);
binWriter.Write(lookupDir);
binWriter.Write(autoSaveTime);
binWriter.Write(showStatusBar);
}
finally {
if ( binWriter != null ) {
binWriter= null;
}
}
} //Close
} //AppSettings
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
Show: