using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.IO; using LFP_Manager.DataStructure; using LFP_Manager.Utils; namespace LFP_Manager.Function { class csIniControlFunction { #region [Function] INI File Read Function(Section Setting) public static string[] GetIniValue(string Section, string path) { byte[] ba = new byte[5000]; uint Flag = GetPrivateProfileSection(Section, ba, 5000, path); return Encoding.Default.GetString(ba).Split(new char[1] { '\0' }, StringSplitOptions.RemoveEmptyEntries); } #endregion #region [Function] INI File Read Function(Section and Key Setting) public static string GetIniValue(string Section, string key, string path) { StringBuilder sb = new StringBuilder(500); int Flag = GetPrivateProfileString(Section, key, "", sb, 500, path); return sb.ToString(); } #endregion #region [Function] INI File Read Function(Section, Key, Value, Address Setting) public bool SetIniValue(string Section, string Key, string Value, string path) { return (WritePrivateProfileString(Section, Key, Value, path)); } #endregion #region [DLL Function] INI DLL Load [DllImport("kernel32")] public static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName); [DllImport("kernel32")] public static extern bool WritePrivateProfileString(string lpAppName, string lpKeyName, string lpString, string lpFileName); [DllImport("kernel32")] public static extern uint GetPrivateProfileInt(string lpAppName, string lpKeName, int nDefault, string lpFileName); [DllImport("kernel32")] public static extern uint GetPrivateProfileSection(string lpAppName, byte[] lpPairValues, uint nSize, string lpFileName); [DllImport("kernel32")] public static extern uint GetPrivateProfileSectionNames(byte[] lpSections, uint nSize, string lpFileName); #endregion public static DateTime Delay(int MS) { DateTime ThisMoment = DateTime.Now; TimeSpan duration = new TimeSpan(0, 0, 0, 0, MS); DateTime AfterWards = ThisMoment.Add(duration); while (AfterWards >= ThisMoment) { System.Windows.Forms.Application.DoEvents(); ThisMoment = DateTime.Now; } return DateTime.Now; } public static void IniLoad(string AppPath, ref CommConfig rConfig) { string path; path = System.IO.Path.GetDirectoryName(AppPath); if (File.Exists(String.Format("{0}\\CommSet.ini", path))) { StringBuilder ret = new StringBuilder(); string InIPath = String.Format("{0}\\CommSet.ini", path); // CommType = 0: SNMP, 1: RS-485 rConfig.CommType = (int)csIniControlFunction.GetPrivateProfileInt("COMM TYPE", "TYPE", 0, InIPath); // Serial Port Config // PORT ret.Clear(); csIniControlFunction.GetPrivateProfileString("UART", "PORT", "(NONE)", ret, 32, InIPath); rConfig.UartPort = ret.ToString(); // MODEL rConfig.UartModelIndex = (int)csIniControlFunction.GetPrivateProfileInt("UART", "MODEL", 0, InIPath); // Module Qunatity = 1 - 16 rConfig.ModuleQty = (int)csIniControlFunction.GetPrivateProfileInt("UART", "MODULE_QTY", 1, InIPath); // PROTOCOL rConfig.UartProtocol = (int)csIniControlFunction.GetPrivateProfileInt("UART", "PROTOCOL", 0, InIPath); // RECV_WAIT_TIME rConfig.RecvWaitTime = (int)csIniControlFunction.GetPrivateProfileInt("UART", "RECV_WAIT_TIME", 50, InIPath); // Etc. Config rConfig.DbLogPeriod = (int)csIniControlFunction.GetPrivateProfileInt("DATABASE", "LOG_PERIOD", 5, InIPath); // Etc. Config rConfig.RealTimeChartMaxCount = (int)csIniControlFunction.GetPrivateProfileInt("CHART", "MAX_COUNT", (2 * 60 * 60), InIPath); // Gyro. Config rConfig.GyroSensitive = (int)csIniControlFunction.GetPrivateProfileInt("GYRO", "SENSITIVITY", 50, InIPath); // ENABLE PASSWORD ret.Clear(); csIniControlFunction.GetPrivateProfileString("PW", "ENABLE", "6003", ret, 256, InIPath); string rawEnablePw = ret.ToString(); rConfig.EnablePassword = csCrypto.Decrypt(rawEnablePw); // DISABLE PASSWORD ret.Clear(); csIniControlFunction.GetPrivateProfileString("PW", "DISABLE", "7003", ret, 256, InIPath); string rawDisablePw = ret.ToString(); rConfig.DisablePassword = csCrypto.Decrypt(rawDisablePw); // 초기 기동 시 파일에 저장된 비밀번호가 평문이면 즉시 암호화하여 파일 갱신 if (!csCrypto.IsEncrypted(rawEnablePw)) { WritePrivateProfileString("PW", "ENABLE", csCrypto.Encrypt(rConfig.EnablePassword), InIPath); } if (!csCrypto.IsEncrypted(rawDisablePw)) { WritePrivateProfileString("PW", "DISABLE", csCrypto.Encrypt(rConfig.DisablePassword), InIPath); } } else { rConfig.CommType = 0; rConfig.UartPort = ""; rConfig.UartProtocol= 0; rConfig.RecvWaitTime = 50; rConfig.DbLogPeriod = 5; rConfig.RealTimeChartMaxCount = (2 * 60 * 60); rConfig.GyroSensitive = 50; rConfig.EnablePassword = "6003"; rConfig.DisablePassword = "7003"; } } public static void IniSave(string AppPath, CommConfig aConfig) { string path = Path.GetDirectoryName(AppPath); string InIPath = String.Format("{0}\\CommSet.ini", path); // write ini // CommType = 0: SNMP, 1: RS-485 WritePrivateProfileString("COMM TYPE", "TYPE", aConfig.CommType.ToString(), InIPath); // Serial Port Config // PORT WritePrivateProfileString("UART", "PORT", aConfig.UartPort, InIPath); // MODEL WritePrivateProfileString("UART", "MODEL", aConfig.UartModelIndex.ToString(), InIPath); // MODULE QTY = 1 - 16 WritePrivateProfileString("UART", "MODULE_QTY", aConfig.ModuleQty.ToString(), InIPath); // PROTOCOL WritePrivateProfileString("UART", "PROTOCOL", aConfig.UartProtocol.ToString(), InIPath); // RECV_WAIT_TIME WritePrivateProfileString("UART", "RECV_WAIT_TIME", aConfig.RecvWaitTime.ToString(), InIPath); // Etc Config WritePrivateProfileString("DATABASE", "LOG_PERIOD", aConfig.DbLogPeriod.ToString(), InIPath); // Etc Config WritePrivateProfileString("CHART", "MAX_COUNT", aConfig.RealTimeChartMaxCount.ToString(), InIPath); // Gyro Config WritePrivateProfileString("GYRO", "SENSITIVITY", aConfig.GyroSensitive.ToString(), InIPath); // ENABLE PASSWORD (암호화하여 저장) WritePrivateProfileString("PW", "ENABLE", csCrypto.Encrypt(aConfig.EnablePassword), InIPath); // DISABLE PASSWORD (암호화하여 저장) WritePrivateProfileString("PW", "DISABLE", csCrypto.Encrypt(aConfig.DisablePassword), InIPath); } } }