Welcome to MSDN Blogs Sign in | Join | Help

Favorties.html

Long long ago, when IE was still in version 1.0, most people, including me, used Netscape, where favorites were not a bunch of files hidden somewhere, but rather an HTML page. Few of my friends used to make this page a browser's start page -- quite convenient thing to do. I missed it, but got to writing a simple program that would generate it. Well, now, with .Net and C# it's so-o-o-o easy! So, I don't have that excuse anymore :-)

Here is a C# console application that generates such a file out of your Favorites folder. It finds Favorites automatically and drops the result in a folder from where you called it. You may call it from the StartUp to make sure it's updated each time you reboot. Use favorites.css (see the end of this post) to control how your favorites.html page will look like.

Merely compile this C# code (command line will do, or create a simple console C# application in Visual Studio, I used .Net 2.0) and have fun!

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace GrabFavorites
{
    class Program
    {
        static StreamWriter html;

        static String FindFavorites()
        {
            String s = System.Environment.GetEnvironmentVariable("USERPROFILE");
            s += "
\\Favorites\\";
            return s;
        }

        static String GenLink(FileInfo file)
        {
            String url = "";
            char[] delim = new char[]{'='};

            StreamReader f = file.OpenText();
            while (!f.EndOfStream)
            {
                String line = f.ReadLine();
                String[] parts = line.Split(delim,2);
                if (parts.Length > 1 && parts[0].Equals("URL", StringComparison.InvariantCultureIgnoreCase))
                {
                    url = parts[1];
                    break;
                }
            }
            f.Close();
            String filename = file.Name;
            filename = filename.Remove(filename.Length - 4);
            return "<a href='"+url+"'>"+filename+"</a>";
        }

        static void WalkFavorites(String path, int level)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            if (!dir.Exists)
            {
                Console.WriteLine("Problems with directory '{0}'...", dir.FullName);
                return; // Oops... should break here...
            }
           
            FileInfo[] files = dir.GetFiles("*.url");
            foreach (FileInfo file in files)
            {
                html.WriteLine("<p class='line{0}'>{1}</p>", level, GenLink(file));
            }
            DirectoryInfo[] dirs = dir.GetDirectories();
            foreach (DirectoryInfo dr in dirs)
            {
                html.WriteLine("<p class='dir{0}'>{1}</p>",level,dr.Name);
                WalkFavorites(dr.FullName, level+1);
            }

        }

        static void Main(string[] args)
        {
            // Find Favorites
            String FavoritesPath = FindFavorites();
            html = new StreamWriter("favorites.html");

            html.WriteLine("<head><link rel='stylesheet' type='text/css' href='favorites.css' /></head>");
            html.WriteLine("<h1>Favorites</h1>");

            WalkFavorites(FavoritesPath,0);

            // Console.ReadKey();
            html.Close();
        }
    }
}

Favorites.css (change it the way you like, and drop into the same folder, where favorites.html is generated)

h1  { color: blue; }
.dir0        { font-size: 14pt; font-family: Arial; font-weight: bold; margin-top: 6;
               margin-bottom: 1 }
.dir1        { margin-left: 0.3in; text-indent: 0; font-size: 12pt; font-family: Arial; font-weight: bold; margin-top: 6;
               margin-bottom: 1 }
.dir2        { margin-left: 0.6in; text-indent: 0; font-size: 11pt; font-family: Arial; font-weight: bold; margin-top: 3;
               margin-bottom: 1 }
.dir3        { margin-left: 0.9in; text-indent: 0; font-size: 10pt; font-family: Arial; font-weight: bold; margin-top: 1;
               margin-bottom: 1 }
.dir4        { margin-left: 1.2in; text-indent: 0; font-size: 10pt; font-family: Arial; font-weight: bold; margin-top: 1;
               margin-bottom: 1 }
.dir5        { margin-left: 1.5in; text-indent: 0; font-size: 10pt; font-family: Arial; font-weight: bold; margin-top: 1;
               margin-bottom: 1 }
.dir6        { margin-left: 1.8in; text-indent: 0; font-size: 10pt; font-family: Arial; font-weight: bold; margin-top: 1;
               margin-bottom: 1 }
.dir7        { margin-left: 2.1in; text-indent: 0; font-size: 10pt; font-family: Arial; font-weight: bold; margin-top: 1;
               margin-bottom: 1 }
.line0        { margin-left: 0.3in; margin-top: 0; margin-bottom: 1 }
.line1        { margin-left: 0.6in; margin-top: 0; margin-bottom: 1 }
.line2        { margin-left: 0.9in; margin-top: 0; margin-bottom: 1 }
.line3        { margin-left: 1.2in; margin-top: 0; margin-bottom: 1 }
.line4        { margin-left: 1.5in; margin-top: 0; margin-bottom: 1 }
.line5        { margin-left: 1.8in; margin-top: 0; margin-bottom: 1 }
.line6        { margin-left: 2.1in; margin-top: 0; margin-bottom: 1 }
.line7        { margin-left: 2.4in; margin-top: 0; margin-bottom: 1 }

Published Thursday, March 02, 2006 10:43 AM by EldarM
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Favorties.html

very good!
Saturday, April 29, 2006 11:54 AM by serghei

# College Fun Facts &raquo; Random Thoughts and Hints on Software Development : Favorties.html

# Random Thoughts and Hints on Software Development Favorties html | Green Tea Fat Burner

# Random Thoughts and Hints on Software Development Favorties html | Cellulite Creams

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker