参考:
https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader?view=netframework-4.8
https://docs.microsoft.com/en-us/dotnet/api/system.io.binarywriter?view=netframework-4.8
读 / 写二进制文件的类。
参考代码:
using System; using System.IO; class ConsoleApplication { const string fileName = "AppSettings.dat"; static void Main() { WriteDefaultValues(); DisplayValues(); } public static void WriteDefaultValues() { using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create))) { writer.Write(1.250F); writer.Write(@"c:Temp"); writer.Write(10); writer.Write(true); } } public static void DisplayValues() { float aspectRatio; string tempDirectory; int autoSaveTime; bool showStatusBar; if (File.Exists(fileName)) { using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open))) { aspectRatio = reader.ReadSingle(); tempDirectory = reader.ReadString(); autoSaveTime = reader.ReadInt32(); showStatusBar = reader.ReadBoolean(); } Console.WriteLine("Aspect ratio set to: " + aspectRatio); Console.WriteLine("Temp directory is: " + tempDirectory); Console.WriteLine("Auto save time set to: " + autoSaveTime); Console.WriteLine("Show status bar: " + showStatusBar); } } }