using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.IO;

namespace Fm.Bussiness
{
    public class IOStream
    {

        public IOStream()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 单个文件删除
        /// </summary>
        /// <param name="path"></param>
        public bool DelFile(string path)
        {


            if (File.Exists(path))
            {
                //
                File.Delete(path);
                
                return true;
            }
            else
            {
                return false;
            }


        }
        /// <summary>
        /// 更新文件夹名称
        /// </summary>
        /// <param name="path">原名称</param>
        /// <param name="file">现有名称</param>
        public void SaveFile(string path, string file)
        {
           // FileInfo file = new FileInfo();
            DirectoryInfo df = new DirectoryInfo(path);
            if(!df.Exists)
            {
                df.Create();
            }
            if (path != file)
            {
                df.MoveTo(file);
            }
           // Directory.Move(path,file);

        }
        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="path"></param>
        public void CreateFile(string path)
        {
            DirectoryInfo df = new DirectoryInfo(path);
            if (!df.Exists)
            {
                df.Create();
            }
          
        }
        //读取流
        public string ReaderIo(string path)
        {
            Encoding code = Encoding.GetEncoding("gb2312");
            StreamReader Reader = new StreamReader(path, code);
            //  file.OpenText();
            string str = "";
            try
            {
                str = Reader.ReadToEnd();
                return str;
            }
            catch
            {
                return "";
            }
            finally
            {
                Reader.Close();
            }

        }

        //写入流
        public StreamWriter WriterIo(string path, string content)
        {

            Encoding code = Encoding.GetEncoding("gb2312");
            FileInfo file = new FileInfo(path);
            if (file.Exists)
            {
                file.Delete();
            }

            StreamWriter writer = null;
            try
            {

                writer = new StreamWriter(path, false, code);
                writer.Write(content);

                writer.Flush();

                
            }
            finally
            { 
               
                writer.Close();
                writer.Dispose();

            }
            return writer;
        }
    }
}
