2

I'm using .NET 2.0 Visual Studio 2005 C#.

The code below gets file name of the IE favorites (bookmark) from the directory that contains bookmarked .url files

Example

../users/favorites/blah.url

But what I really want is the bookmarked URL inside of that file.

When check the file property, in the web document tab, it shows filename and URL.

How can I access it from C#?

CODE

 //the code below just get String like "..../users/favorites/blah.url"
 //call the method with the folder path: 
 //GetFavoriteFiles(Environment.GetFolderPath(Environment.SpecialFolder.Favorites));


private List<String> favFiles = new List<String>();

private void GetFavoriteFiles(String folder)
{
    String[] favs = Directory.GetFiles(folder);
    favFiles.AddRange(favs);
    String[] folders = Directory.GetDirectories(folder);

    if(folders != null)
    {
       foreach(String s in folders)
       {
          GetFavoriteFiles(s);
       }
    }
}
Meow
  • 18,371
  • 52
  • 136
  • 180

2 Answers2

6

The current format of a .url file is not set in stone and could change in any OS update. The right way to parse these files is via the CLSID_InternetShortcut COM coclass, using IUniformResourceLocator and IPropertyStorage. I just added that capability to TvGameLauncher, you can take the code from the InternetShortcut folder (Apache 2.0 License).

Sample usage:

var shortcut = new InternetShortcutManaged(@"MyShortcut.url");

Console.WriteLine("URL: " + shortcut.Url);
Console.WriteLine("Working dir: " + shortcut.WorkingDir);
Console.WriteLine("Icon file: " + shortcut.IconFile);
Console.WriteLine("Icon index: " + shortcut.IconIndex);
Console.WriteLine("Name: " + shortcut.Name);
Console.WriteLine("Description: " + shortcut.Description);
Console.WriteLine("Comment: " + shortcut.Comment);
Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
  • If this answer had references to the namespaces and a bit more information as to what and why you used the code you did I would really like this answer. – Rusty Nail Jul 28 '14 at 08:34
  • @Chris I linked to the specific MSDN article detailing all the internet shortcut related COM information. I also explained why this method is preferred, namely that the current INI format is an implementation detail that can change at any point, rendering old parsing code useless. Finally I added a link to working open source code you can take and use as is with virtually no license limitations, and even supplied a small sample of exactly how to use it. What more could you possibly ask? – Ohad Schneider Jul 28 '14 at 09:40
5

I opened a .url in Notepad++ and this is what I found. Note, this was generated in IE8. This page has a detailed look into the format of the .url (internet shortcut) file.

[DEFAULT]
BASEURL=http://www.google.com.au/
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2
[InternetShortcut]
URL=http://www.google.com.au/
IDList=
IconFile=http://www.google.com.au/favicon.ico
IconIndex=1

You should be able to parse this easily using basic StreamReader IO.

Alastair Pitts
  • 19,423
  • 9
  • 68
  • 97
  • Oh! thats what I should have done in the first place, thanks I will try that. – Meow Jun 22 '11 at 02:15
  • 1
    Note that this format is an implementation detail, and is not guaranteed to remain as it is. See my answer for the proper API to use for parsing internet shortcuts. – Ohad Schneider Jul 12 '14 at 18:00