我正在使用 .NET 2.0 Visual Studio 2005 C#。
下面的代码从包含书签的 .url 文件的目录中获取 IE 收藏夹(书签)的文件名
例子
../users/favorites/blah.url
但我真正想要的是该文件中带有书签的 URL。
当检查文件属性时,在网络文档选项卡中,它显示文件名和 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);
}
}
}
最佳答案
.url
文件的当前格式是 not set in stone并且可以在任何操作系统更新中更改。解析这些文件的正确方法是通过 CLSID_InternetShortcut COM coclass ,使用 IUniformResourceLocator
和 IPropertyStorage
。我刚刚将该功能添加到 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);
关于c# - 如何访问 .URL 文件中的 URL 和书签标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6434126/