C#把对象序列化成二进制文件

来源:互联网 发布:java判断质数的方法 编辑:程序博客网 时间:2024/05/01 12:18

static void SaveAsBinaryFormat(object obj,string filename)
        {
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binFormat =
                                     new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

            using(System.IO.Stream fsStream = new System.IO.FileStream(filename,System.IO.FileMode.Create,
                                System.IO.FileAccess.Write,System.IO.FileShare.None))
            {
                binFormat.Serialize(fsStream, obj);
            }
        }

0 0
原创粉丝点击