C#

Ini File class

안녕1999 2020. 10. 10. 23:45
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;
using System.IO.Ports;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class IniFile
{
	static String m_ini_file;
	String m_Section;
	static StringBuilder temp;
	const int buf_size = 1024 * 4;
	public IniFile()
	{
		if (temp == null)
		{
			m_ini_file = System.Reflection.Assembly.GetExecutingAssembly().Location;
			m_ini_file=m_ini_file.Replace(".exe", ".ini");
			temp = new StringBuilder(buf_size);
		}
		else
		{ }

		m_Section = "Data";
	}

	[DllImport("kernel32.dll")]
	private static extern int GetPrivateProfileString(
		String section, String key, String def, StringBuilder retVal, int Size, String filePat);

	[DllImport("Kernel32.dll")]
	private static extern long WritePrivateProfileString(
		String Section, String Key, String val, String filePath);

	public string GetIniFileName()
	{
		return m_ini_file;
	}
	public void SetIniFileName(string inifile)
	{
		m_ini_file = inifile;
	}
	public void SetSection(string Section)
	{
		m_Section = Section;
	}
	public void Puts(string Key, string Value)
	{
		string s=Gets(Key, "");
		if (s != Value)
		{
			WritePrivateProfileString(m_Section, Key, Value, m_ini_file);
		}
		else
		{
			//동일함.skip
		}
	}
	public void Put_int(string Key, int Value)
	{
		int a = Get_int(Key, 0);
		if (a != Value)
		{
			WritePrivateProfileString(m_Section, Key, Convert.ToString(Value), m_ini_file);
		}
		else
		{
			//동일함.skip
		}
	}
	public void Put_double(string Key, double Value)
	{
		double a = Get_double(Key, 0.0f);
		if (a != Value)
		{
			WritePrivateProfileString(m_Section, Key, Convert.ToString(Value), m_ini_file);
		}
		else
		{
			//동일함.skip
		}
	}

	public string Gets(string Key, string default_value)
	{
		string s;
		int i = GetPrivateProfileString(m_Section, Key, "", temp, buf_size, m_ini_file);
		if (temp.Length == 0)
		{
			s = default_value;
		}
		else
		{
			s=temp.ToString();
		}
		return s;
	}
	public int Get_int(string Key,int default_value)
	{
		string s=Gets(Key,Convert.ToString(default_value));
		return Convert.ToInt32(s);
	}
	public double Get_double(string Key,double default_value)
	{
		string s = Gets(Key, Convert.ToString(default_value));
		return Convert.ToDouble(s);
	}
}