using System;
using System.Xml;
using System.Linq;
using System.Xml.Linq;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using AR;
using System.IO;
using System.Text;
using System.Drawing;


namespace vmsnet
{
    public static class MethodExts
    {
        /// <summary>
        /// 지정된 파일 끝에 내용을 추가합니다.
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="data"></param>
        public static void AppendText(this System.IO.FileInfo filename, string data)
        {
            if (filename.Directory.Exists == false) filename.Directory.Create();
            using (var writer = new StreamWriter(filename.FullName, true, System.Text.Encoding.Default))
                writer.Write(data);//
        }
        public static void AppendText(this System.IO.FileInfo filename, StringBuilder data)
        {
            AppendText(filename, data.ToString());
        }
       

        /// <summary>
        /// 파일에 내용을 기록합니다.
        /// 파일지 존재하면 덮어쓰기 됩니다.
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="data"></param>
        public static void WriteText(this System.IO.FileInfo filename, StringBuilder data, bool append = false)
        {
           WriteText(filename, data.ToString(), append);
        }
        public static void WriteText(this System.IO.FileInfo filename, string data, bool append = false)
        {
            if (filename.Directory.Exists == false) filename.Directory.Create();
            if (append == false)
                System.IO.File.WriteAllText(filename.FullName, data);
            else
                AppendText(filename, data);
        }
        public static string GetData(this XElement el, string key, string elName, string defvalue = "")
        {
            if (el == null) return defvalue;
            if (el.HasElements == false) return defvalue;

            var KeyItem = el.Element(key);
            if (KeyItem == null || KeyItem.HasElements == false) return defvalue;

            var item = KeyItem.Element(elName);
            if (item == null) return defvalue;

            if (item.IsEmpty) return defvalue;

            return item.Value;
        }
    }

}
