如何访问.URL文件中的URL和书签标题?
我正在使用.NET 2.0 Visual Studio 2005 C#。
下面的代码从包含书签.url文件的目录中获取IEcollections夹(书签)的文件名
例
../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 favFiles = new List(); 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); } } }
我在Notepad ++中打开了一个.url
,这就是我找到的。 注意,这是在IE8中生成的。 此页面详细介绍了.url
(Internet快捷方式)文件的格式。
[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轻松解析它。
.url
文件的当前格式不是 .url
,可能会在任何操作系统更新中发生变化。 解析这些文件的正确方法是使用IUniformResourceLocator
和IPropertyStorage
通过CLSID_InternetShortcut COM coclass 。 我刚刚将该function添加到TvGameLauncher ,您可以从InternetShortcut文件夹 (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);