导航
  • 主页
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm
  • WPF
  • 问答

C#文件(读写)操作

lxcooi 2004-08-08 02:51:51
现有一结构:
public struct test
{
public uint a;
public char b;
public long c;
public bool d;
}

我想将这个结构的值存入一个数据文件中(我不想用文本方式)如何作呢?
相对的,写之后还是需要读出,并填充这个结构。
谢谢!
...全文
209 点赞 收藏 4
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxcooi 2004-08-08
感谢您们回答我的问题--C#文件(读写)操作
我按您的答案作了一个测试,但我发现序列化后的流比我要存储的信息大很多!
我用了另一种方式来解决:
  写入时通过System.BitConverter.GetBytes()和Encoding.Default.GetBytes()将要存放的数据转换成字符数组。然后存入文件。读出时通过相应的反函数来解码。
  以下是我的程序,不足之处请指教:
using System;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;

namespace FilePack
{
public class FilePack
{
[System.Serializable]
public struct PackHead
{
public char[] tag;
public uint length;
public uint block;
public uint count;
public byte[] getByteArray()
{
MemoryStream stm = new MemoryStream();
stm.Write(Encoding.Default.GetBytes(tag), 0, 4);
stm.Write(BitConverter.GetBytes(length),0,4);
stm.Write(BitConverter.GetBytes(block),0,4);
stm.Write(BitConverter.GetBytes(count),0,4);
return stm.ToArray();
}
}

public FilePack()
{

}

public bool CreatePack(string packFileName)
{
PackHead pHead = new PackHead();
FileStream fs;

pHead.tag = ".PCK".ToCharArray();
pHead.block = 1024;
fs = File.Create(packFileName);
fs.Write(pHead.getByteArray(), 0, 16);
fs.Close();
return true;
}

public PackHead OpenPack(string packFileName)
{
byte[] buf = new byte[16];
PackHead pHead = new PackHead();

FileStream fs;
fs = File.Open(packFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
fs.Read(buf, 0, 16);
pHead.tag = "....".ToCharArray();
Encoding.Default.GetChars(buf,0,4,pHead.tag,0);
pHead.length = BitConverter.ToUInt32(buf, 4);
pHead.block = BitConverter.ToUInt32(buf, 8);
pHead.count = BitConverter.ToUInt32(buf, 12);
fs.Close();
return pHead;
}
}
}
回复
triout 2004-08-08
首先,这个结构必须是SERIALIZE的:

[System.Serializable]
public struct test
{
public uint a;
public char b;
public long c;
public bool d;
}


其次,要保存这个结构必须把这个结构转换为序列化数据的数组(你可以根据需要进行其他必要的处理):
Test test=new Test();
MemoryStream streamMemory = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(streamMemory, test);
byte[] bytesTest=streamMemory.GetBuffer();

这样就可以把bytesTest数组进行保存了。

第三,从数组中获取对象

先要获得该结构保存后的序列数据的byte数组bytesTest,然后:
MemoryStream streamMemory = new MemoryStream(bytesTest);
Test test=(Test)formatter.Deserialize(streamMemory);
回复
huangsuipeng 2004-08-08
给个例子你
序列化对象进DB
UserInfo userInfo;
MemoryStream objMemoryStream;
BinaryFormatter objBinary;
userInfo = new UserInfo(txtName.Text,txtPassword.Text,txtEmail.Text);
objMemoryStream = new MemoryStream();
objBinary = new BinaryFormatter();
objBinary.Serialize(objMemoryStream,userInfo);
cmd = new SqlCommand("insert UserList(username,userinfo) values(@username,@userinfo)",conn);
cmd.Parameters.Add("@username",SqlDbType.VarChar,50).Value = txtName.Text;
cmd.Parameters.Add("@userinfo",SqlDbType.Image).Value = objMemoryStream.ToArray();
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
labInfo.Text = "Serialize succeseed!";


///反序列化操作
UserInfo userInfo;
MemoryStream objMemoryStream;
BinaryFormatter objBinary;
SqlDataReader rs;
byte[] arrByte;
cmd = new SqlCommand("select top 1 * from UserList where username=@username",conn);
conn.Open();
rs = cmd.ExecuteReader();
if(rs.Read())
{
arrByte = (byte[])(rs["userinfo"]);
objMemoryStream = new MemoryStream(arrByte);
objBinary = new BinaryFormatter();
try
{
userInfo = (UserInfo)(objBinary.Deserialize(objMemoryStream));
txtPassword.Text = userInfo.PassWord;
txtEmail.Text = userInfo.Email;
labInfo.Text = "Deserialize successed!";
}
catch(SerializationException ee)
{
labInfo.Text = ee.Message;
}
}
else
labInfo.Text = "Not found user by :" + txtName.Text;
conn.Close();
}
回复
八爪鱼-杭州 2004-08-08
实现序列化
回复
发动态
发帖子
C#
创建于2007-09-28

10.4w+

社区成员

.NET技术 C#
申请成为版主
社区公告

全世界最好的语言,没有之一.