各个函数的作用我就不解释了,从名字上就可以看出来.

也希望大家用的时候如果发现错误请指正

 1 // IniFile.h: interface for the CIniFile class.
 2 //
 3 //////////////////////////////////////////////////////////////////////
 4 
 5 #if !defined(AFX_INIFILE_H__3EF6D649_6870_480B_BA94_D135F75D8C2A__INCLUDED_)
 6 #define AFX_INIFILE_H__3EF6D649_6870_480B_BA94_D135F75D8C2A__INCLUDED_
 7 
 8 #if _MSC_VER > 1000
 9 #pragma once
10 #endif // _MSC_VER > 1000
11 
12 class CIniFile  
13 {
14 public:
15  CString GetClassVersion();
16  DWORD IniFile_GetKeyNames(CString strSectionName, CStringArray &strArray);
17 
18  UINT GetMaxSize_A_Section();
19  UINT GetMaxSize_All_SectionNames();
20 
21  DWORD IniFile_GetKeyNamesValues(CString strSectionName,CStringArray &strArray);
22  BOOL  IniFile_GetStruct(LPCTSTR lpSectionName,LPCTSTR lpKeyName,LPVOID lpStruct,UINT uSizeStruct);
23  DWORD IniFile_GetSectionNames(CStringArray &strArray );
24 
25  BOOL  IniFile_DeleteStruct(LPCTSTR lpSectionName, LPCTSTR lpKeyName);
26  BOOL  IniFile_DeleteSection(CString strSectionName);
27  BOOL  IniFile_DeleteString(CString strSectionName,CString strKeyName);
28 
29  BOOL  IniFile_ModifyString(LPCTSTR lpSectionName,LPCTSTR lpKeyName,LPCTSTR lpString);
30  BOOL  IniFile_ModifyInt(LPCTSTR lpSectionName, LPCTSTR lpKeyName, int nValue);
31 
32 
33  BOOL  IniFile_WriteStruct(LPCTSTR lpSectionName,LPCTSTR lpKeyName,LPVOID lpStruct,UINT uSizeStruct);
34  BOOL  IniFile_WriteSection(LPCTSTR lpSectionName, LPCTSTR lpString);
35  BOOL  IniFile_WriteLongString(LPCTSTR lpValue,LPCTSTR lpSectionName,LPCTSTR lpKeyName,  ...);
36 
37  BOOL  IniFile_WriteString(LPCTSTR lpSectionName,LPCTSTR lpKeyName,LPCTSTR lpString);
38  BOOL  IniFile_WriteInt(LPCTSTR lpSectionName, LPCTSTR lpKeyName, int nValue);
39 
40  DWORD IniFile_GetString(CString strSectionName,CString strKeyName,CString strDefault,CString &strReturnedString);
41  DWORD IniFile_GetString(LPCTSTR lpSectionName,LPCTSTR lpKeyName,LPCTSTR lpDefault,LPTSTR lpReturnedString,DWORD nSize);
42  UINT  IniFile_GetInt(LPCTSTR lpSectionName,LPCTSTR lpKeyName,INT nDefault);
43 
44 
45  BOOL    EnsureIniFileExist();
46 
47  CString GetFilePathName();
48  void    SetDefaultFilePathName();
49  BOOL    SetFileName(LPCTSTR lpFileName);
50 
51  void    SetMaxSize_All_SectionNames(UINT nSize);
52  void    SetMaxSize_A_Section(UINT nSize );
53 
54  CIniFile();
55  virtual ~CIniFile();
56  
57 protected:
58  BOOL    IsIniFileExist();
59  void    IniFile_InitializeForCreate();
60 
61  DWORD   IniFile_GetSectionNames(LPTSTR lpszReturnBuffer, DWORD nSize);
62  DWORD   IniFile_GetSection(LPCTSTR lpSectionName,LPTSTR lpReturnedString, DWORD nSize);
63  
64 
65 private:
66  CString m_strFileFullName;
67  UINT    nMaxSize_All_SectionNames;
68  UINT    nMaxSize_A_Section;
69 };
70 
71 #endif // !defined(AFX_INIFILE_H__3EF6D649_6870_480B_BA94_D135F75D8C2A__INCLUDED_)

 

  1 // IniFile.cpp: implementation of the CIniFile class.
  2 //
  3 //////////////////////////////////////////////////////////////////////
  4 
  5 #include "stdafx.h"
  6 
  7 #include "IniFile.h"
  8 
  9 #ifdef _DEBUG
 10 #undef THIS_FILE
 11 static char THIS_FILE[]=__FILE__;
 12 #define new DEBUG_NEW
 13 #endif
 14 
 15 //////////////////////////////////////////////////////////////////////
 16 // Construction/Destruction
 17 //////////////////////////////////////////////////////////////////////
 18 
 19 CIniFile::CIniFile()
 20 {
 21  nMaxSize_All_SectionNames = 1024;
 22  nMaxSize_A_Section = 1024;
 23  SetDefaultFilePathName();
 24 }
 25 
 26 CIniFile::~CIniFile()
 27 {
 28 
 29 }
 30 
 31 BOOL CIniFile::IsIniFileExist()
 32 {
 33  CFile MyFile;
 34  if (!MyFile.Open(m_strFileFullName,CFile::modeRead))
 35   return 0;
 36  MyFile.Close(); 
 37  return 1;
 38 }
 39 
 40 BOOL CIniFile::EnsureIniFileExist()
 41 {
 42  if (IsIniFileExist())
 43   return 1;
 44 
 45  CFile MyFile;
 46 
 47  if (!MyFile.Open(m_strFileFullName,CFile::modeCreate))
 48   return FALSE;
 49 
 50  MyFile.Close(); 
 51 
 52  IniFile_InitializeForCreate();
 53  
 54  return TRUE;
 55 }
 56 
 57 void CIniFile::SetDefaultFilePathName()
 58 {
 59  TCHAR exeFullPath[MAX_PATH];
 60  int len2 = GetModuleFileName(NULL,exeFullPath,MAX_PATH);
 61 
 62  CString strDir(exeFullPath);
 63  strDir.TrimLeft();
 64  strDir.TrimRight();
 65 
 66  CString strTemp = strDir.Left(strDir.GetLength() - 3);
 67  strDir = strTemp;
 68  strDir += _T("ini");
 69  m_strFileFullName = strDir;
 70  return;
 71 }
 72 
 73 BOOL CIniFile::IniFile_WriteString(LPCTSTR lpSectionName, 
 74           LPCTSTR lpKeyName, 
 75           LPCTSTR lpString)
 76 {
 77  return WritePrivateProfileString(lpSectionName,
 78     lpKeyName,
 79     lpString,
 80     m_strFileFullName);
 81 }
 82 
 83 BOOL CIniFile::IniFile_WriteInt(LPCTSTR lpSectionName,
 84        LPCTSTR lpKeyName,
 85        int nValue)
 86 {
 87  CString strTemp;
 88  strTemp.Format(_T("%d"),nValue);
 89  return WritePrivateProfileString(lpSectionName,
 90     lpKeyName,
 91     strTemp,
 92     m_strFileFullName);
 93 }
 94 
 95 UINT CIniFile::IniFile_GetInt(LPCTSTR lpSectionName, 
 96         LPCTSTR lpKeyName, 
 97         INT nDefault)
 98 {
 99  return GetPrivateProfileInt(lpSectionName,
100     lpKeyName,
101     nDefault,
102     m_strFileFullName);
103 }
104 
105 DWORD CIniFile::IniFile_GetString(LPCTSTR lpSectionName,
106          LPCTSTR lpKeyName,
107          LPCTSTR lpDefault,
108          LPTSTR lpReturnedString,
109          DWORD nSize)
110 {
111  return GetPrivateProfileString(lpSectionName,
112     lpKeyName,
113     lpDefault,
114     lpReturnedString,
115     nSize,
116     m_strFileFullName);
117 }
118 
119 DWORD CIniFile::IniFile_GetString(CString strSectionName,
120          CString strKeyName,
121          CString strDefault,
122          CString &strReturnedString)
123 {
124 
125  char buf[256];
126  DWORD len = GetPrivateProfileString(strSectionName,
127     strKeyName,
128     strDefault,
129     buf,
130     256,
131     m_strFileFullName);
132  buf[len] = 0;
133 
134  strReturnedString.Format("%s",buf); 
135  return len;
136 }
137 
138 CString CIniFile::GetFilePathName()
139 {
140  return m_strFil

 

 

posted on 2010-10-26 19:41  °ι 、曲 终  阅读(433)  评论(0)    收藏  举报