ARTICLE

Exporting IE Bookmarks/Favorites Programmatically / .NET

Posted by Bhushan Gawale Articles | Learn .NET August 09, 2011
This article shows how we can export the Internet Explorer's bookmarks or favorites to a HTML file using a simple .NET application
Reader Level:

 We all know that when we mark any URL as a favorite using the UI in IE then it gets saved in the favorites folder of the user. To take a look, I opened the favorites folder and found that all the shortcuts are saved in the form of a .url file. These .url files are nothing but the Internet Shortcuts which points to the location of the actual URL.

There is an option to export all the bookmarked URLs either to XML or HTML format in Firefox (at least in the latest version) also google chrome has this option to export all bookmarks to HTML and also for IE there is option to import or export settings as File>Import and Export. but what If we want to do it programatically?

So, I have tried creating a utility which exports all your bookmarked URLs to a HTML file.

So just to try, I opened the developer's heaven (VS) and started by creating a sample console application. And here is the code which I managed to do what I was thinking.

The HTML file gets created in the same directory where you are executing the exe.

There might be other ways to do this but I pasted what I have done. So your views and suggestions are always welcome J

class Program

{

  private static string _favouritesPath = string.Empty;

  private static string _previousDirectoryName = string.Empty;

  private static string _currentDirectory = string.Empty;

  private static string _fileName = string.Empty;

 

  static void Main(string[] args)

  {

    try

    {

      _favouritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

      if (!string.IsNullOrEmpty(_favouritesPath))

      {

        DirectoryInfo di = new DirectoryInfo(_favouritesPath);

        _currentDirectory = Environment.CurrentDirectory;

        TextWriter _writer = new StreamWriter(_currentDirectory + "\\BookMarks.htm", false);

 

        if (di != null)

        {

          FileInfo[] _files = di.GetFiles("*.url", SearchOption.AllDirectories);

          if (_files != null && _files.Length > 0)

          {

           foreach (FileInfo _file in _files)

           {

             if (_file.Exists)

             {

               _fileName = _file.Name.Split('.').FirstOrDefault().ToString();

               StreamReader _reader = _file.OpenText();

               string _allContents = _reader.ReadToEnd();

               string[] _splits = _allContents.Split(new char[] { '=', '[' }, StringSplitOptions.RemoveEmptyEntries);

               if (_splits.Length > 0 && !string.IsNullOrEmpty(_splits[1]))

               {

                 if (!string.Equals(_file.Directory.Name, _previousDirectoryName))

                 {

                   _writer.Write("</br>");

                   //<b style="color: Green;"></b>

                   _writer.Write("<b style=\"color: Green;\">" + _file.Directory.Name + "</b>");

                   _writer.Write("</br>");

                    _writer.Write(string.Format("{0}<a href=\"{1}\">{2}</a>", " ------ ", _splits[1], _fileName));

                 }

                else

                {

                  _writer.Write("</br>");

                  _writer.Write(string.Format("{0}<a href=\"{1}\">{2}</a>", " ------ ", _splits[1], _fileName));

 

                }

 

              }

             _previousDirectoryName = _file.Directory.Name;

            }

           }

          }

         }

 

         _writer.Close();

 

       }

      }

      catch (Exception ex)

      {

        Console.WriteLine(string.Format("{0}-{1}", "Error:", ex.Message));

      }

       Console.WriteLine("File Created..press any key to exit..");                   
       
Console.ReadLine();

     }

    }

Login to add your contents and source code to this article
post comment
     

Great post. Keep writing more. Have idea for firefox and chrome?negaboys http://www.negablog.com

Posted by nega testac Jan 20, 2013

Hi Mike, Thanks for the comment and hint yes I tried IE Import and Export option and works pretty well to export the book marks , but this article just an example to get started for those who wants to do more customizations on generated file with small piece .NET code. also this code might helps guys to understand how we can extract contents from .url or short cut files. Thanks again .. have a nice day.. :)

Posted by Bhushan Gawale Aug 10, 2011

In IE, try "File | Import and export...". I think that does what you are looking for. It is however not very flexible and your article can help someone to write something that is more flexible.

Posted by Sam Hobbs Aug 09, 2011
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.