//收藏夹太多太混乱,打算系统整理合并.整个工具未开发完成
//下面是读取系统默认收藏夹文件中所有包含子目录中的链接网址信息代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FavoriteManager
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
string FavoritePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Favorites);
LoadLinkFolders(new System.IO.DirectoryInfo(FavoritePath));
}
/// <summary>
/// 遍历系统收藏夹目录中所有链接网址
/// </summary>
/// <param name="ParentFolder"></param>
private void LoadLinkFolders(DirectoryInfo ParentFolder)
{
string ParentFolderName = ParentFolder.Name;
foreach (FileInfo SubFile in ParentFolder.GetFiles())
{
string strCurrentLinkUrl = GetLinkFileUrl(SubFile.FullName);
Console.WriteLine("当前文件" + SubFile.FullName + "链接网址为" + strCurrentLinkUrl);
}
foreach (DirectoryInfo SubFolder in ParentFolder.GetDirectories())
{
try
{
LoadLinkFolders(SubFolder);
}
catch (Exception)
{
Console.WriteLine("读取当前文件夹" + SubFolder.FullName + "收藏网址失败!");
}
}
}
/// <summary>
/// 根据收藏夹URL文件获取对应的链接网址
/// </summary>
/// <param name="linkFilePath"></param>
/// <returns></returns>
private string GetLinkFileUrl(string linkFilePath)
{
string strReturn = "";
//异常检测开始
try
{
FileStream fs = new FileStream(linkFilePath, FileMode.Open, FileAccess.Read);//读取文件设定
StreamReader myStreamReader = new StreamReader(fs, System.Text.Encoding.Default);//设定读写的编码
//使用StreamReader类来读取文件
myStreamReader.BaseStream.Seek(0, SeekOrigin.Begin);
string strLine = myStreamReader.ReadLine();
while (strLine != null)
{
if (strLine.IndexOf("URL=") == 0)
{
strReturn = strLine.Replace("URL=", "");
break;
}
strLine = myStreamReader.ReadLine();
}
//关闭此StreamReader对象
myStreamReader.Close();
}
catch
{
strReturn = "";
}
return strReturn;
}
}
}