【问题标题】:How to access URL and bookmark title in .URL files?如何访问 .URL 文件中的 URL 和书签标题?
【发布时间】:2011-09-20 00:27:30
【问题描述】:

我正在使用 .NET 2.0 Visual Studio 2005 C#。

下面的代码从包含书签的 .url 文件的目录中获取 IE 收藏夹(书签)的文件名

例子

../users/favorites/blah.url

但我真正想要的是该文件中带有书签的 URL。

当检查文件属性时,在 web 文档选项卡中,它会显示文件名和 URL。

如何从 C# 访问它?

代码

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

【问题讨论】:

    标签: c# .net internet-explorer url favorites


    【解决方案1】:

    我在 Notepad++ 中打开了一个.url,这就是我发现的。请注意,这是在 IE8 中生成的。 This page 详细查看了.url(互联网快捷方式)文件的格式。

    [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
    

    您应该能够使用基本的StreamReader IO 轻松解析它。

    【讨论】:

    • 哦!那是我一开始就应该做的,谢谢,我会试试的。
    • 请注意,此格式是一个实现细节,不能保证保持原样。有关用于解析 Internet 快捷方式的正确 API,请参阅我的答案。
    【解决方案2】:

    .url 文件的当前格式为not set in stone,并且可能在任何操作系统更新中发生变化。解析这些文件的正确方法是通过CLSID_InternetShortcut COM coclass,使用IUniformResourceLocatorIPropertyStorage。我刚刚将该功能添加到TvGameLauncher,您可以从InternetShortcut folder(Apache 2.0 许可证)获取代码。

    示例用法:

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

    【讨论】:

    • 如果这个答案有对命名空间的引用以及关于你使用什么以及为什么使用你所做的代码的更多信息,我真的很喜欢这个答案。
    • @Chris 我链接到特定的 MSDN 文章,详细介绍了所有与 Internet 快捷方式相关的 COM 信息。我还解释了为什么首选这种方法,即当前的 INI 格式是一个可以随时更改的实现细节,使旧的解析代码无用。最后,我添加了一个指向工作开源代码的链接,您可以按原样获取和使用,几乎没有许可证限制,甚至还提供了一个关于如何使用它的小示例。你还能问什么?
    猜你喜欢
    • 2012-04-24
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    相关资源
    最近更新 更多