文件流的操作

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;
            }

        }
    }
}

 

posted @ 2015-03-18 17:31  流浪的狸猫  阅读(171)  评论(0编辑  收藏  举报