using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;

namespace Ant.Service.Utilities
{
    /// <summary>
    /// INI�ļ���д�ࡣ
    /// </summary>
    public class INIClass
    {

        public string inipath;
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        /// <summary>

        /// ���췽��

        /// </summary>

        /// <param name="INIPath">����</param>

        public INIClass(string INIPath)
        {
            inipath = INIPath;
        }

        /// <summary>

        /// д��INI�ļ�

        /// </summary>

        /// <param name="Section">��Ŀ����(�� [TypeName] )</param>

        /// <param name="Key">��</param>

        /// <param name="Value">ֵ</param>
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.inipath);
        }

        /// <summary>

        /// ����INI�ļ�

        /// </summary>

        /// <param name="Section">��Ŀ����(�� [TypeName] )</param>

        /// <param name="Key">��</param>

        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
            return temp.ToString();
        }

        /// <summary>

        /// ��֤�ļ��Ƿ����

        /// </summary>

        /// <returns>����ֵ</returns>

        public bool ExistINIFile()
        {
            return File.Exists(inipath);
        }
        /// <summary> 
        /// 
        ///�������
        /// </summary>  
        /// <param name="path">
        ///·��
        ///</param> 
        private void NewDirectory(String path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }
        /// <summary> 
        /// ����һ��ע��

        /// </summary>  
        /// <param name="Notes">ע��</param> 
        public void AddNotes(string Notes)
        {
            string filename = inipath;
            string path;
            path = Directory
            .GetParent(filename).ToString();
            NewDirectory(path);
            FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.BaseStream.Seek(0, SeekOrigin.End);
            sw.WriteLine(@";" + Notes);


            sw.Flush();
            sw.Close();
            fs.Close();
            sw.Dispose();
            fs.Dispose();
        }
        /// <summary> 
        /// ����һ���ı�
        /// </summary>  
        /// <param name="Text">�ı�</param> 
        public void AddText(string Text)
        {
            string filename = inipath;
            string path;
            path = Directory
            .GetParent(filename).ToString();
            NewDirectory(path);
            FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.BaseStream.Seek(0, SeekOrigin.End);
            sw.WriteLine(Text);
            sw.Flush();
            sw.Close();
            fs.Close();
            sw.Dispose();
            fs.Dispose();
        }


    }


}