Exporting IE Bookmarks/Favorites Programmatically / .NET


 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();

     }

    }


Similar Articles