码迷,mamicode.com
首页 > 其他好文 > 详细

文件流的操作

时间:2015-03-18 20:09:54      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Command
{
    public class FilesOperations
    {

        public List<string> GetFilesByExtension(string Extension)
        {
            DirectoryInfo folder = new DirectoryInfo(@"D:\wms");

            foreach (FileInfo file in folder.GetFiles("*." + Extension))
            {
                return GetTXT(file);
            }
            return null;
        }

        private List<string> GetTXT(FileInfo fileinfo)
        {
            List<string> txt = new List<string>();
            if (System.IO.File.Exists(fileinfo.DirectoryName + "\\" + fileinfo.Name.Split(.)[0] + ".ok"))
            {
                var file = System.IO.File.Open(fileinfo.FullName, FileMode.Open);

                using (var stream = new StreamReader(file))
                {
                    while (!stream.EndOfStream)
                    {
                        txt.Add(stream.ReadLine());
                    }
                }


                //操作完成把操作的文件包括同名.ok文件删除
                //将 文件 移动到其他目录
                System.IO.File.Move(fileinfo.DirectoryName + "\\" + fileinfo.Name.Split(.)[0] + ".ok", @"D:\BackupWms\" + fileinfo.Name.Split(.)[0] + ".ok");
                fileinfo.MoveTo(@"D:\BackupWms" + "\\" + fileinfo.Name);


                file.Close();
            }
            return txt;
        }


    }

    public class FileWrite
    {
        private string fn;
        public FileWrite(string fileNmae)
        {
            fn = fileNmae;
        }
        public bool Files(string s)
        {
            try
            {
                StreamWriter sw = new StreamWriter(fn, true, Encoding.UTF8);
                sw.WriteLine(s);
                sw.Flush();
                sw.Close();
                StreamWriter ok = new StreamWriter(fn.Split(.)[0] + ".ok", true, Encoding.UTF8);
                ok.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }
    }
}

 

文件流的操作

标签:

原文地址:http://www.cnblogs.com/liuchang/p/4347866.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!