定义了类
public class st_Tmp_Set
{
public byte a1;
public byte a2;
public byte a3;
public st_Tmp_Set(){}
}
public st_Tmp_Set Tmp_Set = new st_Tmp_Set();
然后要将Tmp_Set里面的内容用二进制形式写到文件里面去,应该怎么写?相当于vb中的
    Open FilePath & FileName For Binary Access Write As #IntFile
            Put #IntFile, , Tmp_Set
    Close #IntFile

解决方案 »

  1.   

    BinaryWriter 和 BinaryReader 类用于读取和写入数据,而不是字符串。下面的代码示例示范了向新的、空文件流 (Test.data) 写入数据及从该文件读取数据。在当前目录中创建了数据文件之后,也就同时创建了相关的 BinaryWriter 和 BinaryReader,BinaryWriter 用于向 Test.data 写入整数 0 到 10,Test.data 在文件尾留下了一个文件指针。在将文件指针设置回初始位置后,BinaryReader 读出指定的内容。[Visual Basic]
    Option Explicit On 
    Option Strict On
    Imports System
    Imports System.IO
    Class MyStream
        Private Const FILE_NAME As String = "Test.data"
        Public Shared Sub Main()
            ' Create the new, empty data file.
            If File.Exists(FILE_NAME) Then
                Console.WriteLine("{0} already exists!", FILE_NAME)
                Return
            End If
            Dim fs As New FileStream(FILE_NAME, FileMode.CreateNew)
            ' Create the writer for data.
            Dim w As New BinaryWriter(fs)
            ' Write data to Test.data.
            Dim i As Integer
            For i = 0 To 10
                w.Write(CInt(i))
            Next i
            w.Close()
            fs.Close()
            ' Create the reader for data.
            fs = New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
            Dim r As New BinaryReader(fs)
            ' Read data from Test.data.
            For i = 0 To 10
                Console.WriteLine(r.ReadInt32())
            Next i
        w.Close()
        End Sub
    End Class[C#]
    using System;
    using System.IO;
    class MyStream 
    {
        private const string FILE_NAME = "Test.data";
        public static void Main(String[] args) 
        {
            // Create the new, empty data file.
            if (File.Exists(FILE_NAME)) 
            {
                Console.WriteLine("{0} already exists!", FILE_NAME);
                return;
            }
            FileStream fs = new FileStream(FILE_NAME, FileMode.CreateNew);
            // Create the writer for data.
            BinaryWriter w = new BinaryWriter(fs);
            // Write data to Test.data.
            for (int i = 0; i < 11; i++) 
            {
                w.Write( (int) i);
            }
            w.Close();
            fs.Close();
            // Create the reader for data.
            fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
            BinaryReader r = new BinaryReader(fs);
            // Read data from Test.data.
            for (int i = 0; i < 11; i++) 
            {
                Console.WriteLine(r.ReadInt32());
            }
            w.Close();
        }
    }如果 Test.data 已存在于当前目录中,则引发一个 IOException。始终使用 FileMode.Create 创建新文件,而不引发 IOException。
      

  2.   

    using System;
    using System.IO;
    using System.Collections;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Runtime.Serialization;public class App 
    {
        [STAThread]
        static void Main() 
        {
            Serialize();
            Deserialize();
        }    static void Serialize() 
        {
            // Create a hashtable of values that will eventually be serialized.
            Hashtable addresses = new Hashtable();
            addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052");
            addresses.Add("Fred", "987 Pine Road, Phila., PA 19116");
            addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301");        // To serialize the hashtable and its key/value pairs,  
            // you must first open a stream for writing. 
            // In this case, use a file stream.
            FileStream fs = new FileStream("DataFile.dat", FileMode.Create);        // Construct a BinaryFormatter and use it to serialize the data to the stream.
            BinaryFormatter formatter = new BinaryFormatter();
            try 
            {
                formatter.Serialize(fs, addresses);
            }
            catch (SerializationException e) 
            {
                Console.WriteLine("Failed to serialize. Reason: " + e.Message);
                throw;
            }
            finally 
            {
                fs.Close();
            }
        }   
        static void Deserialize() 
        {
            // Declare the hashtable reference.
            Hashtable addresses  = null;        // Open the file containing the data that you want to deserialize.
            FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
            try 
            {
                BinaryFormatter formatter = new BinaryFormatter();            // Deserialize the hashtable from the file and 
                // assign the reference to the local variable.
                addresses = (Hashtable) formatter.Deserialize(fs);
            }
            catch (SerializationException e) 
            {
                Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
                throw;
            }
            finally 
            {
                fs.Close();
            }        // To prove that the table deserialized correctly, 
            // display the key/value pairs.
            foreach (DictionaryEntry de in addresses) 
            {
                Console.WriteLine("{0} lives at {1}.", de.Key, de.Value);
            }
        }
    }
      

  3.   

    to fanzai(帆仔):
            for (int i = 0; i < 11; i++) 
            {
                w.Write( (int) i);
            }
    需要写入的是整个结构的内容,里面包含上百个byte定义,不知道总的大小,需要整体一块写进去。to hdt(倦怠):
    结构是自己定义的,好像不能执行formatter.Serialize(fs, 结构名);
    会出错,说不能Serialize
      

  4.   

    [System.Serializable]
    public class st_Tmp_Set
    {
    public byte a1;
    public byte a2;
    public byte a3;
    public st_Tmp_Set(){}
    }
      

  5.   

    to hdt(倦怠):
    谢谢,刚刚我是少写了[System.Serializable]
      

  6.   

    to hdt(倦怠):
    formatter.Serialize(fs, st_Tmp_Set);后,
    a1,a2,a3也写入文件了,
    有什么办法只写入a1,a2,a3的内容,不写入变量名呢?
      

  7.   

    比如a1=0x01 a2=0x02 a3=0x03
    我要写进文件的是000000020003
      

  8.   

    自定义序列化
    ms-help://MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemruntimeserializationiserializableclassgetobjectdatatopic.htm
      

  9.   


    public void AAA(DataSet ds)
    {
        MemoryStream memStream = new MemoryStream();
        IFormatter formatter = new BinaryFormatter();    ds.RemotingFormat = SerializationFormat.Binary;
        formatter.Serialize(memStream, ds);
        byte[] binaryResult = memStream.ToArray();    FileStream fileStream = new FileStream(@"D:\test", FileMode.CreateNew);
        BinaryWriter writer = new BinaryWriter(fileStream);
        writer.Write(binaryResult);    writer.Close();
        memStream.Close();
        fileStream.Close();
    }在.net 1.1下DataTable是不能被序列化的,在.net 2.0里才支持。