using System; using System.Runtime.InteropServices; using System.Text; namespace NewPosInstaller { /// /// Win32 Ini file read/write /// 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) /// /// Read Data(String) From the Ini File /// /// /// /// /// /// 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) /// /// Read Data(Integer) From the Ini File /// /// /// /// /// /// 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) /// /// Read Data(Dobule) From the Ini File /// /// /// /// /// /// 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) /// /// Write Data to the INI File /// /// /// /// /// /// 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 } }