如何在C#中读取和写入二进制文件中的字符串数组? [英] How to Read and Write String Array in Binary File in C# ?

查看:143
本文介绍了如何在C#中读取和写入二进制文件中的字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个应用程序,我需要在其中存储和读取二进制文件中的字符串数组以及同一二进制文件中的其他数据类型。

我的数据字段是: -

  string  []天; 
string msg;
日期时间;
字符串 FilePath;





如何保存(写入)并读取所有上述数据字段值在一个二进制文件中???



感谢高级!!!



有问候

Jayanta ..

解决方案

 使用 System.Runtime.Serialization.Formatters.Binary; 
使用 System.IO;



  private   void  Test()
{
string file = C:\\ file;
object ob1 = GetSomeObject();
byte [] ba = ConvertObjectToByteArray(ob1);
File.WriteAllBytes(file,ba);

byte [] ba2 = File.ReadAllBytes(file);
object ob2 = ConvertByteArrayToObject(ba);
}



  public   static   byte  [] ConvertObjectToByteArray( object  ob)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms,ob);
return ms.ToArray();
}

public static object ConvertByteArrayToObject( byte [] ba)
{
BinaryFormatter bf = new BinaryFormatter();
流stream = new MemoryStream(ba);
return bf.Deserialize(stream);
}



更新

将字符串写为二进制文件:

  string  file =   C:\\file; 
string string1 = John | Gold Membership | RegisterDate = 2013\" 年12月13日;
byte [] ba = Encoding.UTF8.GetBytes(string1);
File.WriteAllBytes(file,ba);

byte [] ba2 = File.ReadAllBytes(file);
string string2 = Encoding.UTF8.GetString(ba2);

MessageBox.Show(string2);


您可以使用BinaryFormatter类来完成。雅,有很多解决方案适合你的问题,但更复杂的序列化你可以做到。见下文。

BinaryFormatter与手动序列化 [< a href =http://www.codeproject.com/Articles/311944/BinaryFormatter-or-Manual-serializingtarget =_ blanktitle =New Window> ^ ]


访问这里..





http://www.pcreview.co.uk/forums/read-write-binary-file-data-string-t3936880.html [ ^ ]

Hi,
I create a application in which I need to stored and read the string array in a Binary file and also others data type in the same binary file.
My Data Fields are :-

string[] Days;
string msg;
DateTime time;
String FilePath;



How do I save(write) and read all above data Fields value in one binary file???

Thanks In Advanced!!!

With Regards
Jayanta..

解决方案

using System.Runtime.Serialization.Formatters.Binary;
using System.IO;


private void Test()
{
    string file = "C:\\file";
    object ob1 = GetSomeObject();
    byte[] ba = ConvertObjectToByteArray(ob1);
    File.WriteAllBytes(file, ba);

    byte[] ba2 = File.ReadAllBytes(file);
    object ob2 = ConvertByteArrayToObject(ba);
}


public static byte[] ConvertObjectToByteArray(object ob)
{
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    bf.Serialize(ms, ob);
    return ms.ToArray();
}

public static object ConvertByteArrayToObject(byte[] ba)
{
    BinaryFormatter bf = new BinaryFormatter();
    Stream stream = new MemoryStream(ba);
    return bf.Deserialize(stream);
}


Update
Write string as binary file:

string file = "C:\\file";
string string1 = "John|Gold Membership|RegisterDate=2013-12-13";
byte[] ba = Encoding.UTF8.GetBytes(string1);
File.WriteAllBytes(file,ba);

byte[] ba2 = File.ReadAllBytes(file);
string string2 = Encoding.UTF8.GetString(ba2);

MessageBox.Show(string2);


You can do it with BinaryFormatter class. Ya, there are many solutions fit to you question but more sophisticated serializing you can do it. See below.
BinaryFormatter vs. Manual Serializing[^]


visit here..


http://www.pcreview.co.uk/forums/read-write-binary-file-data-string-t3936880.html[^]


这篇关于如何在C#中读取和写入二进制文件中的字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆