# CodeGuru Technical FAQs > CodeGuru Individual FAQs >  MFC Registry: How to store the application settings in registry?

## ovidiucucu

*Q*: How to store and retrieve the MFC application settings in/from registry?

*A*: By using the following CWinApp member functions:
'CWinApp::GetProfileString()''CWinApp::GetProfileInt()'CWinApp::WriteProfileString()'CWinApp::WriteProfileInt()'
Before calling any of above functions, you have to call 'CWinApp::SetRegistryKey()'. This causes application settings to be stored in the registry instead of regualr INI files.

Further, 'CWinApp' registry functions will read/write under 'HKEY_CURRENT_USER\Software\<_'lpszRegistryKey' argument of 'SetRegistryKey()'_>\<_'lpszSection' argument of 'CWinApp' registry function_>'.

Here is an example of an application that stores last "User Name" for login (e.g. to a database) to be used in subsequent application instances:



```
class CMyApp : public CWinApp
{
  // ...
  // Attributes
protected:
  CString m_strUserName;
  LPCTSTR const m_pszLoginSection;
  LPCTSTR const m_pszUserNameEntry;
  // ... 

  // Operations
public:
  void GetUserName(CString& strUserName) const
  {
    strUserName = m_strUserName;
  }

  void SetUserName(LPCTSTR pszNewValue)
  {
    m_strUserName = pszNewValue;
  }

  //...
};

CMyApp::CMyApp() : m_pszLoginSection(_T("Login")),
                   m_pszUserNameEntry(_T("User Name"))
{
}

BOOL CMyApp::InitInstance()
{
  // ...
  // Change the registry key under which our settings are stored.
  // Usually it's the company name.
  SetRegistryKey(_T("Zerolei Software"));

  // Read "User Name" registry entry (value) from 
  // HKEY_CURRENT_USER\Software\Zerolei Software\MyApp\Login key
  m_strUserName = GetProfileString(m_pszLoginSection, m_pszUserNameEntry);

  // ...
  return TRUE;
}

int CMyApp::ExitInstance() 
{
  // write 'm_strUserName' in "User Name" registry entry (value) under
  // HKEY_CURRENT_USER\Software\Zerolei Software\MyApp\Login key
  WriteProfileString(m_pszLoginSection, m_pszUserNameEntry, m_strUserName);

  // ...
  return CWinApp::ExitInstance();
}

void CMainFrame::OnLogin() 
{
  CMyApp* pApp = (CMyApp*)AfxGetApp();
  CLoginDialog dlgLogin;
  pApp->GetUserName(dlgLogin.m_strUserName);

  // ...

  if(IDOK == dlgLogin.DoModal())
  {
    pApp->SetUserName(dlgLogin.m_strUserName);
    //...
  }
}
```

----------

