C#获取我的收藏网站及打开URL
C#获取我的收藏网站及打开URL
扫一扫加作者微信
首先添加以下引用:COM下Windows Script Host Object Model
using System.Runtime.InteropServices;
using IWshRuntimeLibrary;
private void button1_Click(object sender, EventArgs e)
{
//获取我的收藏网址.
this.BuildTree(this.treeView1);
}
private void treeView1_DoubleClick(object sender, EventArgs e)
{
if (treeView1.SelectedNode != null)
{
string file = treeView1.SelectedNode.Tag.ToString();
if (file.Trim() != "") //如果有url地址,打开网页
{
IWshShell_Class shell = new IWshShell_ClassClass();
IWshURLShortcut _shortcut = shell.CreateShortcut(file) as IWshURLShortcut;
webBrowser1.Navigate(_shortcut.TargetPath);
}
}
}
public void BuildTree(TreeView tv)
{
string path = System.Environment.SystemDirectory;
path = path.Substring(0, path.IndexOf("\\") + 1);
path = path + @"Documents and Settings\" + System.Environment.UserName + "\\Favorites";
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo file in dir.GetFiles())
{
TreeNode node = new TreeNode();
node.Text = file.Name; //文件名
node.Tag = file.FullName; //文件路径.
tv.Nodes.Add(node);
}
}
{
//获取我的收藏网址.
this.BuildTree(this.treeView1);
}
private void treeView1_DoubleClick(object sender, EventArgs e)
{
if (treeView1.SelectedNode != null)
{
string file = treeView1.SelectedNode.Tag.ToString();
if (file.Trim() != "") //如果有url地址,打开网页
{
IWshShell_Class shell = new IWshShell_ClassClass();
IWshURLShortcut _shortcut = shell.CreateShortcut(file) as IWshURLShortcut;
webBrowser1.Navigate(_shortcut.TargetPath);
}
}
}
public void BuildTree(TreeView tv)
{
string path = System.Environment.SystemDirectory;
path = path.Substring(0, path.IndexOf("\\") + 1);
path = path + @"Documents and Settings\" + System.Environment.UserName + "\\Favorites";
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo file in dir.GetFiles())
{
TreeNode node = new TreeNode();
node.Text = file.Name; //文件名
node.Tag = file.FullName; //文件路径.
tv.Nodes.Add(node);
}
}
扫一扫加作者微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc
C/S框架网