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.

Saturday, October 25, 2008

Some points on using statement in C# !

* using Statement provides standard way of using objects which implements IDisposable interface.
*generally IDisposable objects are those wraps unmanaged resources in them & hence Dispose() method can be called to release unmanahed resource.
*If IDisposable object is declared & instantiated in using statement then Dispose() method will be automatically called while control leaving the using block.
*The IDisposable can be declared & instantiated as other objects noramally & call the Dispose() method to release unmanaged resources.
*But putting it in using statement ensures that the object is disposed even when any exception occured while calling methods on object.
*This is equivalent to intantiating IDisposable object in try/ catch block & calling Dispose() in finally block.
*With in the using block the object is readonly , it cant reassigned.

Below code shows how to use IDisposable objects

//
using (StreamReader streamreader = File.OpenText(@"FilePath"))

{

string filetext = streamreader.ReadToEnd();

}