close

 

這個很簡單,只需要幾個步驟即可

 

Write :

 

    CString strReadioIndex;
    strReadioIndex.Format(_T("%d"), m_CurRadioBoxSel);
    ::WritePrivateProfileString(_T("NewProjectionReadioSel"), _T("Index"), strReadioIndex, g_UISetting.m_strT6nsmgrInIPath);

 

BOOL WINAPI WritePrivateProfileString(
  _In_ LPCTSTR lpAppName,
  _In_ LPCTSTR lpKeyName,
  _In_ LPCTSTR lpString,
  _In_ LPCTSTR lpFileName
);

Parameters

lpAppName [in]

The name of the section to which the string will be copied. If the section does not exist, it is created. The name of the section is case-independent; the string can be any combination of uppercase and lowercase letters.

lpKeyName [in]

The name of the key to be associated with a string. If the key does not exist in the specified section, it is created. If this parameter is NULL, the entire section, including all entries within the section, is deleted.

lpString [in]

A null-terminated string to be written to the file. If this parameter is NULL, the key pointed to by the lpKeyName parameter is deleted.

lpFileName [in]

The name of the initialization file.

If the file was created using Unicode characters, the function writes Unicode characters to the file. Otherwise, the function writes ANSI characters.

 

※ 在這邊提供一個小技巧,你可以在最開始的時候,把INI的路徑先load一遍起來,放在一個CString的變數裡面 ( g_UISetting.m_strT6nsmgrInIPath ) ,並且將這玩意看你要放在global 也好,放在你每個檔案都會load的地方也好,總而言之,就是load一遍就好,為什麼 ?

 

第一,你不能預期之後你這個程式load ini的路徑是絕對的,所以你絕對不能寫死,既然不能寫死,你就必須用動態的方式去改變這個值,也就是說主程式在哪,你就要從那一層開始當指定路徑。

第二,減少程式的負擔,你想想看,如果你在做WritePrivateProfileString之前,每一次都還要去滾一次回圈,然後去找到這個ini的路徑,這不是增加CPU麻煩的事情嗎 ?  切記一件事,咱們程序員能幹的事,就別勞煩電腦在幫你幹一次,電腦是拿來服務使用者的 !

 

第三,簡單明瞭,少行搞定,看的舒服 ! 

 

 

 

 

Read

m_CurRadioBoxSel = GetPrivateProfileInt(_T("NewProjectionReadioSel"), _T("Index"), 0,g_UISetting.m_strT6nsmgrInIPath);

 

讀的部分則是一行搞定。

Parameters

lpAppName [in]

The name of the section in the initialization file.

lpKeyName [in]

The name of the key whose value is to be retrieved. This value is in the form of a string; theGetPrivateProfileInt function converts the string into an integer and returns the integer.

nDefault [in]

The default value to return if the key name cannot be found in the initialization file.

lpFileName [in]

The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.

Return value

The return value is the integer equivalent of the string following the specified key name in the specified initialization file. If the key is not found, the return value is the specified default value.

Remarks

The function searches the file for a key that matches the name specified by the lpKeyName parameter under the section name specified by the lpAppName parameter. A section in the initialization file must have the following form:

 
 
[section]
key=value
      .
      .
      .

The GetPrivateProfileInt function is not case-sensitive; the strings in lpAppName and lpKeyName can be a combination of uppercase and lowercase letters.

An application can use the GetProfileInt function to retrieve an integer value from the Win.ini file.

The system maps most .ini file references to the registry, using the mapping defined under the following registry key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\IniFileMapping

This mapping is likely if an application modifies system-component initialization files, such as Control.ini, System.ini, and Winfile.ini. In these cases, the function retrieves information from the registry, not from the initialization file; the change in the storage location has no effect on the function's behavior.

The profile functions use the following steps to locate initialization information:

  1. Look in the registry for the name of the initialization file under the IniFileMapping key.
  2. Look for the section name specified by lpAppName. This will be a named value under the key that has the name of the initialization file, or a subkey with this name, or the name will not exist as either a value or subkey.
  3. If the section name specified by lpAppName is a named value, then that value specifies where in the registry you will find the keys for the section.
  4. If the section name specified by lpAppName is a subkey, then named values under that subkey specify where in the registry you will find the keys for the section. If the key you are looking for does not exist as a named value, then there will be an unnamed value (shown as <No Name>) that specifies the default location in the registry where you will find the key.
  5. If the section name specified by lpAppName does not exist as a named value or as a subkey, then there will be an unnamed value (shown as <No Name>) that specifies the default location in the registry where you will find the keys for the section.
  6. If there is no subkey or entry for the section name, then look for the actual initialization file on the disk and read its contents.

When looking at values in the registry that specify other registry locations, there are several prefixes that change the behavior of the .ini file mapping:

  • ! - this character forces all writes to go both to the registry and to the .ini file on disk.
  • # - this character causes the registry value to be set to the value in the Windows 3.1 .ini file when a new user logs in for the first time after setup.
  • @ - this character prevents any reads from going to the .ini file on disk if the requested data is not found in the registry.
  • USR: - this prefix stands for HKEY_CURRENT_USER, and the text after the prefix is relative to that key.
  • SYS: - this prefix stands for HKEY_LOCAL_MACHINE\SOFTWARE, and the text after the prefix is relative to that key.

 

 

 

 

arrow
arrow
    全站熱搜

    Eric 發表在 痞客邦 留言(0) 人氣()