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

namespace ProductionControl.Utils
{
    internal class FileUtil
    {
        /// <summary>
        /// 另存为
        /// </summary>
        /// <param name="defaultFileName">默认文件名</param>
        /// <param name="filter">如 图像文件|*.jpg|图像文件|*.png|所有文件|*.*</param>
        /// <returns></returns>
        public static string saveAsFile(string defaultFileName, string filter= "所有文件|*.*")
        {
            SaveFileDialog s = new SaveFileDialog(); 
            s.Title = "保存文件"; 
            s.Filter = filter; //"图像文件|*.jpg|图像文件|*.png|所有文件|*.*";
            //s.DefaultExt = "图像文件|*.jpg";  //默认扩展名  
            //s.InitialDirectory = @"C:\Users\Administrator\Desktop";  //保存的初始目录

            s.FileName = defaultFileName;//默认文件名
            if (s.ShowDialog() == DialogResult.OK) 
                return s.FileName;

            return "";
        }
        /// <summary>
        /// 选择文件
        /// </summary>
        /// <param name="filter">如 图像文件|*.jpg|图像文件|*.png|所有文件|*.*</param>
        /// <returns></returns>
        public static string selectFile(string filter = "所有文件|*.*", string defaultPath = "")
        { 
            OpenFileDialog fie = new OpenFileDialog();  
            fie.Title = "选择文件";
            //fie.InitialDirectory = (defaultPath != "" ? defaultPath : Path.GetFullPath("."));
            fie.Filter = filter;  //设置文件类型 
            if (fie.ShowDialog() == DialogResult.OK)
                return fie.FileName;

            return ""; 
        }
        /// <summary>
        /// 选择目录
        /// </summary>
        /// <returns></returns>
        public static string selectFolder(string defaultPath="")
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.SelectedPath = (defaultPath != "" ? defaultPath : Path.GetFullPath(".")); 
            if (dlg.ShowDialog() == DialogResult.OK) 
                return dlg.SelectedPath;

            return "";
        }
        /// <summary>
        /// 删除指定目录下文件
        /// </summary>
        /// <param name="defaultPath"></param>
        /// <param name="fileExtend"></param>
        /// <returns></returns>
        public static bool delFilesInFolder(string FolderPath,string fileExtend="*")
        {
            if(!Directory.Exists(FolderPath)) 
                return false;
            string[] files=Directory.GetFiles(FolderPath, fileExtend, SearchOption.TopDirectoryOnly); 
            foreach (string file in files)
                API.DeleteFile(file);
            return true;
        }

        ///  <summary>
        ///   写INI文件
        ///  </summary>
        ///  <param  name="Section">Section</param>
        ///  <param  name="Key">Key</param>
        ///  <param  name="value">value</param>
        public static void WriteIniValue(string filePath, string Section, string Key, string value)
        {
            WINAPI.WritePrivateProfileString(Section, Key, value, filePath);
        }

        ///  <summary>
        ///    读取INI文件指定部分
        ///  </summary>
        ///  <param  name="Section">Section</param>
        ///  <param  name="Key">Key</param>
        ///  <returns>String</returns>  
        public static string ReadIniValue(string filePath, string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = WINAPI.GetPrivateProfileString(Section, Key, "", temp, 255, filePath);
            return temp.ToString().Trim();
        }
    }
}
