首页 > Windows开发 > 详细

c#序列化与反序列化(文本)

时间:2017-03-31 15:35:15      阅读:199      评论:0      收藏:0      [点我收藏+]
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class Serialize
{
    static public void DoSerialize<T>(T obj, string path)
    {
        using (FileStream fs = new FileStream(path, FileMode.Create))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(fs, obj);
        }
    }
    static public T DoDeSerialize<T>(string path)
    {
        FileInfo fi = new FileInfo(path);
        T t;
        try
        {
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                t = (T)formatter.Deserialize(fs);
            }
            return t;
        }
        catch
        {
            return default(T);
        }
    }

    static public void SaveFile(string[] _content, string _path)
    {
        using (FileStream fs = new FileStream(_path, FileMode.Create, FileAccess.Write))
        {
            using (StreamWriter sw = new StreamWriter(fs))
            {
                for (int i = 0; i < _content.Length; i++)
                {
                    sw.WriteLine(_content[i]);
                }
            }
        }
    }
}

  

c#序列化与反序列化(文本)

原文:http://www.cnblogs.com/jephone/p/6651521.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!