?? 12.6.2 win.ini文件的寫入.txt
字號:
12.6.2 Win.ini文件的寫入
在程序中,如果想要向 Win .i ni文件寫入一些初始化的信息,可以使用 WriteProfileString函數
來實現。該函數的作用是將一個字符串復制到 Win .ini文件的指定段中。需要注意的是, Windows
提供這個函數只是為了兼容 16位版本的應用程序,基于 Win32的應用程序應該使用注冊表來存儲初
始化信息。 WriteProfileString函數的原型聲明如下所示。
BOOL WriteProfileString(
LPCTSTR 1pAppName,
LPCTSTR 1pKeyName, .
LPCTSTR lpString
該函數各個參數的含義如下所述:
. lpAppName
指向一個以 O結尾的字符串指針,該字符串包含了將把字符串復制到 Win.ini文件中的那個段的段
名。如果該段不存在,則創建這個段。
. IpKeyName
指向一個以 O結尾的字符串的指針,該字符串包含了一個鍵的名字。如果這個鍵在指定段中還不存
在,則創建這個鍵。如果這個參數為 NULL.則整個段,包括該段中所有的工頁,都將被刪除。
. lpString
指向一個以 O結尾的字符串的指針,該字符串將要被寫入到 Win .ini文件中。如果此
參數為 NULL.則參數 IpKeyName所指定的鍵將被刪除。
下面,我們就在File程序中利用WriteProfileString函數將程序的初始信息寫入win-M文件,并且將
這個操作放置到CFileApp類的Initlnstance函數中,在SetRegistryKey函數調用之后添加如例12-24
所示代碼中加灰顯示的那行代碼。
BOOL CFileApp : : Initlnstance()
AfxEnableControlContainer() ;
// Standard initialization // If you are not using these features and wish to reduce the
size // of your final executable, you should remove from the following // the specific
initialization routines you do not need .
#ifdef AFXDLL Enable3dControls () ; / / Call this when using MFC in a shared OLL #else
Enable3dControlsStatic () ; / / Call this when linking to MFC statically #endif
// Change the registry key under wh工ch our settings are stored . // TOOO: You should modify
this string to be something appropriate // such as the name of your company or organization.
SetReg工stryKey (_T ( "Local AppW工zard-Generated Appl工cations " )) ;
::writeProfileString("http://www.sunxin.or g" , "admin" , "zhangsan");
該代碼將向 Win.ini文件寫入一些信息,創建一個新的段,名稱為: http://www. sumin.org,在其
上創建一個新鍵,鍵名為: admin,其值為zhangsan。
注意:因為在CWinApp中也有一個public~型的WriteProfileString函數,
而且 CFileApp派生于此類,因此,如果想要調用 Win32 API中的 WriteProfile String函數,就需
要在該函數名前加上全局作用域標識符(:: )。
Build并運行File程序。然后打開 Win.ini文件,可以在該文件中看到新添加的段名、鍵名及其值,
如圖 12.15所示。
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -