spc-kiosk-pb/WinEtc/NewPosInstaller/Library/IniFile.cs
2019-06-16 14:12:09 +09:00

130 lines
4.8 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Text;
namespace NewPosInstaller
{
/// <summary>
/// Win32 Ini file read/write
/// </summary>
public class IniFile
{
// INI File Write
[DllImport("kernel32")]
private static extern int WritePrivateProfileString(string section, string key, string val, string filePath);
// INI File Read
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileInt(string section, string key, int defVal, string filePath);
#region public static string GetValue(string filePath, string section, string key, string defaultValue)
/// <summary>
/// Read Data(String) From the Ini File
/// </summary>
/// <param name="filePath"></param>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static string GetValue(string filePath, string section, string key, string defaultValue = "")
{
string retValue = string.Empty;
try
{
StringBuilder sb = new StringBuilder(255);
int ret = IniFile.GetPrivateProfileString(section, key, defaultValue, sb, sb.Capacity, filePath);
if (ret != 0) retValue = sb.ToString().Trim();
if (string.IsNullOrWhiteSpace(retValue)) retValue = defaultValue;
}
catch (Exception ex)
{
ComLog.WriteLog(ComLog.Level.Exception
, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "."
+ System.Reflection.MethodBase.GetCurrentMethod().Name + "()"
, ex.Message);
retValue = string.Empty;
}
return retValue.Trim();
}
#endregion
#region public static int GetValue(string filePath, string section, string key, int defaultValue)
/// <summary>
/// Read Data(Integer) From the Ini File
/// </summary>
/// <param name="filePath"></param>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int GetValue(string filePath, string section, string key, int defaultValue)
{
string retValue = GetValue(filePath, section, key, defaultValue.ToString());
double temp = 0;
if (double.TryParse(retValue, out temp))
return (int)temp;
else
return 0;
}
#endregion
#region public static double GetValue(string filePath, string section, string key, double defaultValue)
/// <summary>
/// Read Data(Dobule) From the Ini File
/// </summary>
/// <param name="filePath"></param>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static double GetValue(string filePath, string section, string key, double defaultValue)
{
string retValue = GetValue(filePath, section, key, defaultValue.ToString());
double temp = 0.0;
if (double.TryParse(retValue, out temp))
return temp;
else
return 0.0;
}
#endregion
#region public static bool SetValue(string filePath, string section, string key, string value)
/// <summary>
/// Write Data to the INI File
/// </summary>
/// <param name="filePath"></param>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool SetValue(string filePath, string section, string key, string value)
{
bool retValue = false;
try
{
int ret = IniFile.WritePrivateProfileString(section, key, value, filePath);
retValue = true;
}
catch (Exception ex)
{
ComLog.WriteLog(ComLog.Level.Exception
, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "."
+ System.Reflection.MethodBase.GetCurrentMethod().Name + "()"
, ex.Message);
retValue = false;
}
return retValue;
}
#endregion
}
}