示例#1
0
        void LoadShortcuts()
        {
            using (var db = new LiteDatabase(@".\sc.db"))
            {
                var collection = db.GetCollection <Shortcut>();
                var list       = collection.FindAll()?.ToList();

                if (list != null)
                {
                    foreach (var shortcut in list)
                    {
                        MemoryStream iconStream = new MemoryStream();
                        var          fs         = db.GetStorage <string>();

                        // method 1
                        //var file = fs.FindById(shortcut.Icon);
                        //file.CopyTo(iconStream);


                        // method 2
                        //fs.Download(shortcut.Icon, @"D:\luzhi.jpg", true);


                        // method 3
                        //var file = fs.FindById(shortcut.Icon);
                        //if (file != null)
                        //{
                        //    using (var fStream = file.OpenRead())
                        //    {
                        //        fStream.CopyTo(iconStream);
                        //        iconStream.Seek(0, SeekOrigin.Begin);
                        //    }
                        //}

                        // 加载图标
                        if (!string.IsNullOrEmpty(shortcut.Icon))
                        {
                            fs.Download(shortcut.Icon, iconStream);
                            iconStream.Seek(0, SeekOrigin.Begin); // 就差这一句

                            shortcut.IconStream = (ImageSource) new ImageSourceConverter().ConvertFrom(iconStream);
                        }
                        else
                        {
                            if (File.Exists(shortcut.Command))
                            {
                                var icon = ShellIcon.GetLargeIcon(shortcut.Command);
                                shortcut.IconStream = icon.ToImageSource();
                            }
                            else
                            {
                                //todo: 默认图标
                            }
                        }
                    }
                }

                this.Datas = list;
            }
        }
示例#2
0
        public void Add(Shortcut shortcut)
        {
            using (var db = new LiteDatabase(@".\sc.db"))
            {
                var collection = db.GetCollection <Shortcut>();
                // 给shortcut加上Id
                shortcut.ShortcutId = ObjectId.NewObjectId();
                collection.Insert(shortcut);


                // 上传图标
                if (!string.IsNullOrEmpty(shortcut.Icon))
                {
                    var storage = db.GetStorage <string>();
                    storage.Upload(shortcut.ShortcutId.ToString(), shortcut.Icon);
                }
                else
                {
                    string key     = shortcut.ShortcutId.ToString();
                    string iconKey = "$/Icons/" + key;
                    shortcut.Icon = iconKey;

                    if (File.Exists(shortcut.Command))
                    {
                        var          icon = ShellIcon.GetLargeIcon(shortcut.Command);
                        MemoryStream ms   = new MemoryStream();
                        icon.Save(ms);

                        var storage = db.GetStorage <string>();
                        storage.Upload(key, iconKey, ms);

                        ms.Dispose();
                    }
                }
            }
        }