// use the following with sqlite3.exe command-line tool to generate the actual html output

// .output favorites.html

// select '<tr><td><a href="' || url || '">' || fullName || '</a></td><td>' || creationTime || '</td><td>' || favoriteGroup || '</td></tr>' from shortcut order by creationTime DESC;

// .output stdout

 

// be sure to add the <TABLE> element to the resultant favorites.html file;

 

// ADO.NET 2.0 provider for SQLite found at http://sqlite.phxsoftware.com/

// compiling...

// csc /target:exe program.cs /reference:C:\sqlite\system.data.sqlite.dll

 

// program.cs

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Runtime.InteropServices;

using System.Data.Common;

using System.Text.RegularExpressions;

 

using SQL = System.Data.SQLite;

 

namespace EnumerateFavorites

{

    class Program

    {

 

        [DllImportAttribute("Kernel32")]

        private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder value, int size, string filepath);

       

        private static string rootPath = string.Empty;

 

        static void Main(string[] args)

        {

            string filePath = @"C:\SQLite\favorites.db";

            if (File.Exists(filePath)) { File.Delete(filePath); }

            CreateFavoritesFile( filePath );

 

            DirectoryInfo di = new DirectoryInfo(System.Environment.GetFolderPath(Environment.SpecialFolder.Favorites));

            rootPath = di.FullName;

 

            using (DbConnection cn = new SQL.SQLiteConnection(string.Format("Data Source={0}", filePath)))

            {

                using (DbCommand cmd = cn.CreateCommand())

                {

                    cn.Open();

 

                    cmd.CommandText = "INSERT INTO Shortcut (favoriteGroup, fullName, creationTime, url) VALUES(?, ?, ?, ?)";

                    cmd.Parameters.Add(cmd.CreateParameter());

                    cmd.Parameters.Add(cmd.CreateParameter());

                    cmd.Parameters.Add(cmd.CreateParameter());

                    cmd.Parameters.Add(cmd.CreateParameter());

 

                    EnumerateFavorites(di, cmd);

                }

            }

        }

 

        private static void CreateFavoritesFile(string fullFilePath)

        {

            if (!File.Exists(fullFilePath))

            {

                SQL.SQLiteConnection.CreateFile(fullFilePath);

                using (DbConnection cn = new SQL.SQLiteConnection(string.Format("Data Source={0}", fullFilePath)))

                {

                    cn.Open();

                    using (DbCommand cmd = cn.CreateCommand())

                    {

                        cmd.CommandText = "CREATE TABLE Shortcut ( favoriteGroup VARCHAR(255),fullName VARCHAR(255)," +

                            "creationTime DATETIME, url VARCHAR(255), PRIMARY KEY ( favoriteGroup, fullName ) )";

                        cmd.ExecuteNonQuery();      

                    }

                }

            }

        }

 

        private static void EnumerateFavorites(DirectoryInfo dii, DbCommand cmd )

        {

            Console.WriteLine(dii.FullName.Replace ( rootPath, "" ) );

            foreach (DirectoryInfo dI in dii.GetDirectories())

            {

                EnumerateFavorites(dI, cmd);

            }

 

            foreach (FileInfo fi in dii.GetFiles())

            {

                string groupName = dii.FullName.Replace(rootPath, "");

                if (string.IsNullOrEmpty(groupName)) groupName = "root";

                cmd.Parameters[0].Value = Regex.Replace(groupName, @"[^ -~]", "");

                string fullName = fi.FullName.Replace(rootPath, "");

                cmd.Parameters[1].Value = Regex.Replace(fullName, @"[^ -~]", "");

                cmd.Parameters[2].Value = fi.CreationTime;

                string shortcut = RetrieveFavoritesDetails("InternetShortcut", "URL", fi.FullName);

                cmd.Parameters[3].Value = Regex.Replace(shortcut, @"[^ -~]", "");

 

                try

                {

                    int numRowsAffected = cmd.ExecuteNonQuery();

                }

                catch (SQL.SQLiteException sqlEx)

                {

                    //ignore primary key constraint violations and just keep adding new favotires

                }

            }

        }

 

        private static string RetrieveFavoritesDetails(string sectionName, string propertyName, string path)

        {

            StringBuilder stringBuilder = new StringBuilder(255);

            GetPrivateProfileString(sectionName, propertyName, String.Empty, stringBuilder, 255, path);

            return stringBuilder.ToString();

        }

 

    }

}