📄 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 + -