Thursday, January 29, 2009

How to access Internet Explorer Favourites programatically through C# code & javascript code ??

This article is about how to access Internet Explorer Favorites programatically from C# & javascript code.When ever a Favorite is added in IE , it is stored as a *.url file in "Favorites" foldler of "Documents & Settings" of Windows drive.This location depends on O/S , but it can be accessed through environment variables.
From C#:
Create a console application in visual studio & copy the following code in main method.
//Code
GetFavoriteFiles(Environment.GetFolderPath(Environment.SpecialFolder.Favorites));
foreach (string s in favFiles)
{
Console.WriteLine(s);
}
Console.Read();

//
in the above code 'GetFavoriteFiles("..")' is method which gets the favorite files in that folder & in sub folders if any. The method looks like this

//Code
static List favFiles = new List();
private static void GetFavoriteFiles(string folder)
{
string[] favs = Directory.GetFiles(folder);
favFiles.AddRange(favs);
string[] folders = Directory.GetDirectories(folder);
if (folders != null)
{
foreach (string s in folders)
{
GetFavoriteFiles(s);
}
}
}

//
The above code gets all the IE favorites in form of *.url files.

JavaScript Code :

var i=0;
var favString="";
var fso;
function Import()
{
try
{
fso=new ActiveXObject("Scripting.FileSystemObject");
if(fso !=null )
{
//Create windows script shell object to access Favorites folder in user system.
var object=new ActiveXObject("WScript.Shell");
var favfolderName=object.SpecialFolders("Favorites")
if(favString=="")
{
GetFavourites(favfolderName);
}
} .
}
catch(err)
{
alert("Security settings to be modified in your browser ")
}
}

//Recursive method which retrieves Favourite Name & URL for a given folder & subfolders. function GetFavourites(Folder)
{
var FavFolder=fso.GetFolder(Folder);
//Gets Favourite Names & URL's for given folder.
var files=new Enumerator(FavFolder.Files)
for(; !files.atEnd() ;files.moveNext())
{
var fil=files.item();
if(fil.Type=="Internet Shortcut")
{
var textReader=fso.OpenTextFile(fil.Path,1,false,-2)

var favtext=textReader.ReadAll()
var start=favtext.indexOf("URL",16)
var stop=favtext.indexOf("\n",start)
favString+=fil.Name.replace(/.url/,"");
favString+=":URL:";
//to separate favourite name & favorite URL
favString+=favtext.substring(start+4,stop-1);
favString+=":NEXT:"; //to separate favorites.
i++;
}
}
//Checks any subfolder exists
var subfolders=new Enumerator(FavFolder.SubFolders)
for(; !subfolders.atEnd() ;subfolders.moveNext())
{
var folder=subfolders.item();
GetFavourites(folder.Path);
}
}
The above javascript code also extracts the URL of the favorite.

3 comments:

HappyNomad said...

This is all you really need:
http://www.codeproject.com/KB/shell/iefavdecon.aspx

madhu said...

@HappyNomad

Thanx for your link.In that article grabbing IE favourites through managed code is demonstrated.This article also shows how to do the same with javascript code.If one wants to grab Favorites through add-on then one can go for managed code.What if that functionality should be the part of web page ? This is where the javascript function helps.Any way thanx for your feedback

Anonymous said...

Oh boy.
Bet you never thought you were ever going to get fucked like this.
But you sure did.
DOT.
She got so fucked that she can't stand up in HSTANDD!
And if you can't stand up for that, wtf is she good FOR?
HAE?
DOT.
She got the HAVEREDE in NDIS!
And that ain't just it.
NO SIR!
DOT.
She got that NSPENA SSTANPA with LA STAMPA in PAGICATE!
DOT.
She the NET with " 57" SAUCE!
And DATIL- DATFEE- OOFDAT- TELLDATE- TELGED-
have now all been " instructed" about " candlestick".
And that's a KILLA WATT.
DOT.
You call him WATOTO?
Or ADUCCA?
It all ends UO the same.
HERSEDOT?
ETORYST?