了解一下BinaryReader 和BinaryWirte基元类型:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace ConsoleApp { class Program { private const string FileName = "AppSetting.txt"; static void Main(string[] args) { WriteDefaultValues(); DisplyValues(); } /// <summary> /// 按顺序写入 /// </summary> 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); } } /// <summary> /// 按顺序写入值进行读取 /// </summary> public static void DisplyValues() { 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("float类型值: " + aspectRatio); Console.WriteLine("字符串类型值: " + tempDirectory); Console.WriteLine("int值: " + autoSaveTime); Console.WriteLine("布尔值: " + showStatusBar); } } } }
任何称为"类"的类型都是引用类型。所有值类型都称为结构或枚举
结构都是抽象类型:System.ValueType的直接派生类;
枚举都是System.Enum抽象对象派生的;后者又从System.ValueType.
时间: 2024-09-29 12:22:40