Pages

Friday, July 29, 2011

Exporting IE Bookmarks/Favorites programmatically / .NET

A thought came in my mind yesterday while I was sitting idle and doing nothing (except browsing on InternetJ).. Thought was to get the list of URLs through .NET code which we save or mark using browser IE as favorites.
We all know that when we mark as any URL as favorite using UI in IE then it gets saved in the favorites folder of user. to take a look , I opened favorites folder and found that all the shortcuts gets saved in the form of .url file. These .url files are nothing but the Internet Shortcuts which points to the location of actual URL.
So just to try some hands on I opened the developer’s heaven (VS) and started by creating a sample console application. And here is the code which I managed to achieve what I was thinking.
there is option to export the all bookmarked URL either to XML or HTML format in Firefox (at least in latest version) also google chrome has this option to export all bookmarks to HTML but when came to IE I didn’t find any option. so I have tried creating a utility which exports all your bookmarked URLs to a HTML file.
HTML file gets created in same directory where you are executing the exe.
There might be different ways doing same but I pasted what I 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();
     }
    }

No comments:

Post a Comment