using System;
using System.Collections.Generic;
using System.IO;

namespace lib
{
    class FileUtil
    {
        public static List<FileInfo> listFile(string dir, string search)
        {
            List<FileInfo> items = new List<FileInfo>();
            DirectoryInfo d = new DirectoryInfo(dir);
            foreach (FileInfo file in d.GetFiles(search))
            {
                items.Add(file);
            }
            return items;
        }

        public static void removeDirectory(string dir, bool remove_dir)
        {
            try
            {
                DirectoryInfo di = new DirectoryInfo(dir);

                foreach (FileInfo file in di.GetFiles())
                {
                    try
                    {
                        file.Delete();
                    }
                    catch (Exception ex) { }
                }
                if (remove_dir)
                    foreach (DirectoryInfo d in di.GetDirectories())
                    {
                        d.Delete(true);
                    }
            }
            catch (DirectoryNotFoundException ex)
            {
            }
        }

        public static void removeFile(string file)
        {
            try
            {
                FileInfo fi = new FileInfo(file);

                fi.Delete();
            }
            catch (Exception ex)
            {
            }
        }

        public static void moveFile(string from, string to)
        {
            try
            {
                File.Move(from, to);
            }
            catch (Exception ex)
            {
            }
        }

        public static void renameFile(string oldName, string newName)
        {
            try
            {
                File.Move(oldName, newName);
            }
            catch (Exception ex)
            {
            }
        }

        public static void mkdir(string dir)
        {
            Directory.CreateDirectory(dir);
        }

        public static bool Write(string outputFile, string data)
        {
            bool ret = false;
            // write out CSV data
            using (var wtr = new StreamWriter(outputFile))
            {
                wtr.Write(data);
                ret = true;
            }

            return ret;
        }
    }
}
