标签归档:收藏夹

[转]C# WebBrowser 收藏夹完全解决方案

做了很久,自己编写的浏览器的收藏夹终于解决了,已经成全实现了收藏夹的功能。下面是我的解决方案,帖出来交流交流。欢迎各位高手拍砖。

 

//获取收藏夹路径
        string favorfolder = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);        //获取系统收藏夹路径

        //构造器
        public Form1()
        {
            ListMenuItem(收藏夹ToolStripMenuItem, new DirectoryInfo(favorfolder));
        }


public void ListMenuItem(ToolStripMenuItem toolstripmenuitem, FileSystemInfo info)             //生成收藏夹菜单的函数,递归使用
        {           
            if (!info.Exists) return;
            DirectoryInfo dir = info as DirectoryInfo;
            //不是目录 
            if (dir == null) return;

            FileSystemInfo[] files = dir.GetFileSystemInfos();
            ToolStripMenuItem[] ShouCangsMenuItem = new ToolStripMenuItem[files.Length];
            for (int i = 0; i < files.Length; i++)
            {
                
                FileInfo file = files[i] as FileInfo;                                
                ShouCangsMenuItem[i] = new System.Windows.Forms.ToolStripMenuItem();
               
                //是文件 
                if (file !=null)
                {
                    if (file.Extension == ".url")
                    {
                        ShouCangsMenuItem[i].Image = global::MyWebBrowser2010.Properties.Resources.ie1;//菜单项图片
                        string str = file.Name;             //获取收藏夹的文件名(都是URL文件)
                        str = str.Remove(str.Length - 4);     //去掉.url后缀名

                        ShouCangsMenuItem[i].Text = str;          //然后赋值给菜单文本

                        StreamReader sr = file.OpenText();//获取文件输入流


                        List src = new List();

                        string source = null;
                        while ((source = sr.ReadLine()) != null)
                        {
                            src.Add(source);//文件所有行添加到List中
                        }

                        ShouCangsMenuItem[i].Tag = src;

                        foreach (string stri in src)
                        {
                            if (stri != null)
                            {
                                if (stri.StartsWith("URL="))
                                {
                                    ShouCangsMenuItem[i].ToolTipText = stri.Remove(0,4);//ToolTipText
                                }
                            }
                        }

                        toolstripmenuitem.DropDownItems.Add(ShouCangsMenuItem[i]);   //生成的子菜单添加到上一级菜单
                        ShouCangsMenuItem[i].Click += new EventHandler(ShouCangsMenuItem_Click);          //为生成的子菜单添加单击消息(可写一个消息处理函数,这里就不例出来了)
                    }
                }                
                //对于子目录,进行递归调用 
                else
                {
                    DirectoryInfo Direct = files[i] as DirectoryInfo;                    
                    ShouCangsMenuItem[i].Text = files[i].Name;
                    ShouCangsMenuItem[i].Image = global::MyWebBrowser2010.Properties.Resources.folder2;//菜单项图片
                    ShouCangsMenuItem[i].ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
                    ShouCangsMenuItem[i].ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.SizeToFit;
                    toolstripmenuitem.DropDownItems.Add(ShouCangsMenuItem[i]);       //生成的子菜单添加到上一级菜单
                    ListMenuItem(ShouCangsMenuItem[i], new DirectoryInfo(Direct.FullName));   //递归使用,生成子菜单
                }
            }
        }

        private void ShouCangsMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            string[] tar=((List)item.Tag).ToArray();
            for (int i = 0; i < tar.Length; i++)
            {
                if (tar[i] != null)
                {
                    if (tar[i].StartsWith("URL="))
                    {
                        getCurrentBrowser().Navigate(tar[i].Remove(0, 4));
                    }
                }
            }
        }

c#为webbrowser添加一个添加到收藏夹功能(快捷方式的制作)

webbrowser没有这功能,只能自己写。

要创建快捷方式需要用到IWshRuntimeLibrary命名空间,在这里我们要USING一下。并在引用里添加一个COM,windows script host object model。

 

private void CreateShortCut()
        {
            string mysite = "http://" + iis + "/path/defaut.aspx";

            //创建Windows Script Host Shell类   
            IWshRuntimeLibrary.IWshShell_Class shell = new IWshRuntimeLibrary.IWshShell_ClassClass();

            if (this.Context.Parameters["ckFavorites"] == "1")
            {
                string fav = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Favorites) + "\\××××××××.url";
                //定义快捷方式文件   
                IWshRuntimeLibrary.IWshURLShortcut shortcut = shell.CreateShortcut(fav) as IWshRuntimeLibrary.IWshURLShortcut;
                shortcut.TargetPath = mysite;
                //保存快捷方式 
                shortcut.Save();
            }
            if (this.Context.Parameters["ckDesktop"] == "1")
            {
                string dsk = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "\\××××××××.url";
                IWshRuntimeLibrary.IWshURLShortcut shortcut = shell.CreateShortcut(dsk) as IWshRuntimeLibrary.IWshURLShortcut;
                shortcut.TargetPath = mysite;
                shortcut.Save();
            }
        }