using System.IO;
using System.Reflection;
using System.Text;

namespace QMAPP.WinForm
{
    public static class FileHelper
    {
        public static string GetApplicationPath()
        {
            string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            path = path.Replace(@"file:\", "");
            if (path.Substring(path.Length - 1, 1) != @"\")
            {
                path += @"\";
            }
            return path;
        }

        public static void Create(string path)
        {
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
        }

        public static bool Exists(string filename)
        {
            filename = GetApplicationPath() + filename;
            return File.Exists(filename);
        }

        public static void WriteFile(string filename, string str, bool isAppand = false)
        {
            filename = GetApplicationPath() + filename;
            var sw = new StreamWriter(filename, isAppand, Encoding.Unicode);
            sw.Write(str);
            sw.Close();
        }


        public static string ReadFile(string filename, Encoding encoding = null)
        {
            if (encoding == null)
                encoding = Encoding.Unicode;
            filename = GetApplicationPath() + filename;
            var sr = new StreamReader(filename, encoding);
            var str = sr.ReadToEnd();
            sr.Close();
            return str;
        }
    }
}