定义了类
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
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
解决方案 »
- 哪里有DXPerience 10.1.4 下载
- 如何防止同一帐户同时登录?
- 爬雪山、过草地,走到夏威夷火山口裹棉袄吃雪糕求教
- 我按照例子做了一个C#的SOCKET服务器端,一段时间内没有客户端连接,隔一段时间再连接为什么就连不上了呢
- 请教一个FTP的问题,十万火急!!!
- 請問DataSet.DataTable.Select()如何實現多個字段排序?
- C#怎么获取下面这种Object 里面的数据! 求具体代码. 谢谢! 每个集合里面的两个字段名称都是一样的.
- 菜鸟问题:关于升级
- 怎么用C#控制某个文件夹的用户权限啊?实在不行就用C++吧。
- visual studio .net 2003 装在哪个操作系统上最好?
- 修改磁盘上文件的摘要
- 关于vs.net生成的winform程序怎么制作卸载程序?
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。
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);
}
}
}
for (int i = 0; i < 11; i++)
{
w.Write( (int) i);
}
需要写入的是整个结构的内容,里面包含上百个byte定义,不知道总的大小,需要整体一块写进去。to hdt(倦怠):
结构是自己定义的,好像不能执行formatter.Serialize(fs, 结构名);
会出错,说不能Serialize
public class st_Tmp_Set
{
public byte a1;
public byte a2;
public byte a3;
public st_Tmp_Set(){}
}
谢谢,刚刚我是少写了[System.Serializable]
formatter.Serialize(fs, st_Tmp_Set);后,
a1,a2,a3也写入文件了,
有什么办法只写入a1,a2,a3的内容,不写入变量名呢?
我要写进文件的是000000020003
ms-help://MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfsystemruntimeserializationiserializableclassgetobjectdatatopic.htm
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里才支持。