博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

使用FileHelper进行文本文件的读写

Posted on 2011-05-25 16:08  海岛  阅读(499)  评论(0编辑  收藏  举报

C盘下文件a.txt:

1,AB,2011.01.01
2,您好,2012.02.02
3,我在测试,2013.03.03

 

 

using FileHelpers;
using FileHelpers.DataLink;
using FileHelpers.RunTime;

namespace MySapAssist
{
    public partial class FormFileHelp : DockContent
    {
        [DelimitedRecord(",")]
        public class Customer
        {
            public int id;

            public string name;

            [FieldConverter(ConverterKind.Date, "yyyy.MM.dd")]
            public DateTime birthDay;
        }
       
        private static FormFileHelp _form = null;
        public static FormFileHelp Instance()
        {
            if (_form == null || _form.IsDisposed)
            {
                _form = new FormFileHelp();
            }
            return _form;
        }
       
        public FormFileHelp()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FileHelperEngine engine = new FileHelperEngine(typeof(Customer));

            Customer[] res = engine.ReadFile(@"c:\a.txt") as Customer[];
            engine.WriteFile(@"c:\b.txt", res);

            int k = 1;
        }
    }

 

嗯,感觉很简单很方便,可以使用于本地的一些缓存操作。