How to access URL and bookmark title in .URL files?

蹲街弑〆低调 提交于 2019-12-21 23:28:31

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/6434126/how-to-access-url-and-bookmark-title-in-url-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!