Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / Visual C++ 9.0

Export Web Browser's Favorites To XML File

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
6 Apr 2010CPOL 19.5K   246   10   2
The aritcle gives you a way to export web browser's favorites.
demo src

Introduction

The article will show you an easy method to export web browser's favorites. As we all know, the function import and export favorites of web browser has been deleted. So we must do this by ourselves.

About the Code

If you want to export the favorites, you must know where they are. They can be found in the *.URL's directory by register. After that, read all the items and get names and URLs. The problem is how to get the URL of *.url files? Actually, it's very easy. "*.url" files are the same as "*.ini" files. So you could manipulate it like you manipulate INI files. Look at the following context:

[DEFAULT]
BASEURL=http://faq.csdn.net/read/196943.html
[InternetShortcut]
URL=http://faq.csdn.net/read/196943.html Modified=D09A951BEAD2CA01A1

Open the *.url files, you will find the snippets.

C++
void CExportDlg::OnExport() 
{
	// TODO: Add your control notification handler code here

	CString strFilePath,favpath;

	// Get the location of favorites.
	HKEY hKEY;
	DWORD type=REG_SZ;
	LPCTSTR path=
	    "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";
	DWORD cbData=80;
	TCHAR buf[512];

	::RegOpenKeyEx(HKEY_CURRENT_USER,path,0,KEY_READ,&hKEY);
	::RegQueryValueEx(hKEY,"Favorites",NULL,&type,(LPBYTE)buf,&cbData);
	::RegCloseKey(hKEY);
	favpath.Format ("%s",buf);
	favpath+="\\*.url";

	GetDlgItem(IDC_EDIT1)->GetWindowText (strFilePath.GetBuffer (256),256);

	BOOL bWorking;
	CFileFind ffd;
	bWorking=ffd.FindFile (favpath);
	CString strUrl;

	if(strFilePath!="")
	{
		CMarkup xml;

		xml.AddElem ("main");
		xml.AddAttrib ("version","2"); 
	         LPSTR url=new char[512];

		while(bWorking)
		{
			bWorking=ffd.FindNextFile ();
			if(!ffd.IsDots()&&!ffd.IsDirectory ())
			{
				GetPrivateProfileString("InternetShortcut",
					"URL",NULL,url,512,ffd.GetFilePath ());

				xml.AddChildElem ("ITEM",url);
				xml.IntoElem(); 

			    xml.AddAttrib ("IsFolder","0");

				CString AttribName;
				AttribName=ffd.GetFileName();
				xml.AddAttrib ("name",AttribName.Left 
					(AttribName.GetLength ()-4));
				xml.OutOfElem ();
			}
		}
		delete [] url;

		CString csXML=xml.GetDoc ();
		CString xmlfile;
		xmlfile=" ";
		xmlfile+=csXML;	
		CFile file(strFilePath,CFile::modeWrite|CFile::modeCreate );
		file.WriteHuge (xmlfile.GetBuffer (5000),xmlfile.GetLength ());
		if(IDOK==AfxMessageBox("Export finished!"))
			this->PostMessage (WM_CLOSE,0,0);
	}
}

CMarkup is a class I found on CodeProject... I would really like to thank the author. Hope it will be useful for you.

History

  • 7th March, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
China China
Secret..

Comments and Discussions

 
GeneralMy vote of 1 Pin
DennisAT7-Apr-10 8:51
DennisAT7-Apr-10 8:51 
GeneralRe: My vote of 1 Pin
Aric Wang7-Apr-10 15:20
Aric Wang7-Apr-10 15:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.