使用win32 建立一個 ini 檔案管理 Class => SetupIni
1: public class SetupIni
2: {
3: public string path;
4: [DllImport("kernel32", CharSet = CharSet.Unicode)]
5: private static extern long WritePrivateProfileString(string section,
6: string key, string val, string filePath);
7:
8: [DllImport("kernel32", CharSet = CharSet.Unicode)]
9: private static extern int GetPrivateProfileString(string section,
10: string key, string def, StringBuilder retVal,
11: int size, string filePath);
12:
13: public void IniWriteValue(string Section, string Key, string Value, string inipath)
14: {
15: WritePrivateProfileString(Section, Key, Value, inipath);
16: }
17: public string IniReadValue(string Section, string Key, string inipath)
18: {
19: StringBuilder temp = new StringBuilder(255);
20: int i = GetPrivateProfileString(Section, Key, "", temp, 255, inipath);
21: return temp.ToString();
22: }
23: public string IniReadValue(string Section, string Key, string Value, string inipath)
24: {
25: StringBuilder temp = new StringBuilder(255);
26: int i = GetPrivateProfileString(Section, Key, Value, temp, 255, inipath);
27: return temp.ToString();
28: }
29: }
使用 SetupIni : 1: private void LoadIniFile()
2: {
3: SetupIni ini = new SetupIni();
4: string iniFilename = Path.Combine(Application.StartupPath, "TVBS_SSNMP.ini");
5: try
6: {
7: if (File.Exists(iniFilename))
8: {
9: IP_AsiGuard1 = ini.IniReadValue("AsiGuard1", "IP", IP_AsiGuard1, iniFilename);
10: IP_AsiGuard2 = ini.IniReadValue("AsiGuard2", "IP", IP_AsiGuard2, iniFilename);
11: IP_IPGuard1 = ini.IniReadValue("IPGuard1", "IP", IP_IPGuard1, iniFilename);
12:
13: ini.IniWriteValue("AsiGuard1", "IP", IP_AsiGuard1, iniFilename);
14: ini.IniWriteValue("AsiGuard2", "IP", IP_AsiGuard2, iniFilename);
15: ini.IniWriteValue("IPGuard1", "IP", IP_IPGuard1, iniFilename);
16: }
17: else //.. 建立新 INI_File
18: {
19: ini.IniWriteValue("AsiGuard1", "IP", IP_AsiGuard1, iniFilename);
20: ini.IniWriteValue("AsiGuard2", "IP", IP_AsiGuard2, iniFilename);
21: ini.IniWriteValue("IPGuard1", "IP", IP_IPGuard1, iniFilename);
22: }
23: }
24: catch (Exception)
25: {
26: // throw;
27: }
28: finally
29: {
30: // .......
31: }
32: }
沒有留言:
張貼留言