public static string GenerateRecycleItemsByJson()
        {
            try
            {
                List <Dictionary <string, string> > RecycleItemList = new List <Dictionary <string, string> >();

                foreach (ShellItem Item in RecycleBin.GetItems())
                {
                    try
                    {
                        if (!Item.IsLink)
                        {
                            Dictionary <string, string> PropertyDic = new Dictionary <string, string>
                            {
                                { "OriginPath", Item.Name },
                                { "ActualPath", Item.FileSystemPath },
                                { "CreateTime", Convert.ToString(((System.Runtime.InteropServices.ComTypes.FILETIME)Item.Properties[Ole32.PROPERTYKEY.System.DateCreated]).ToDateTime().ToBinary()) }
                            };

                            RecycleItemList.Add(PropertyDic);
                        }
                    }
                    finally
                    {
                        Item.Dispose();
                    }
                }

                return(JsonConvert.SerializeObject(RecycleItemList));
            }
            catch
            {
                return(string.Empty);
            }
        }
        public static string GenerateRecycleItemsByJson()
        {
            try
            {
                List <Dictionary <string, string> > RecycleItemList = new List <Dictionary <string, string> >();

                foreach (ShellItem Item in RecycleBin.GetItems())
                {
                    try
                    {
                        Dictionary <string, string> PropertyDic = new Dictionary <string, string>
                        {
                            { "ActualPath", Item.FileSystemPath }
                        };

                        if (Path.HasExtension(Item.FileSystemPath))
                        {
                            PropertyDic.Add("StorageType", Enum.GetName(typeof(StorageItemTypes), StorageItemTypes.File));

                            if (Path.HasExtension(Item.Name))
                            {
                                PropertyDic.Add("OriginPath", Item.Name);
                            }
                            else
                            {
                                PropertyDic.Add("OriginPath", Item.Name + Item.FileInfo.Extension);
                            }
                        }
                        else
                        {
                            PropertyDic.Add("OriginPath", Item.Name);
                            PropertyDic.Add("StorageType", Enum.GetName(typeof(StorageItemTypes), StorageItemTypes.Folder));
                        }

                        if (Item.Properties.TryGetValue(Ole32.PROPERTYKEY.System.Recycle.DateDeleted, out object DeleteFileTime))
                        {
                            PropertyDic.Add("DeleteTime", Convert.ToString(((FILETIME)DeleteFileTime).ToInt64()));
                        }
                        else
                        {
                            PropertyDic.Add("DeleteTime", Convert.ToString(DateTimeOffset.MaxValue.ToFileTime()));
                        }

                        RecycleItemList.Add(PropertyDic);
                    }
                    finally
                    {
                        Item.Dispose();
                    }
                }

                return(JsonSerializer.Serialize(RecycleItemList));
            }
            catch
            {
                return(string.Empty);
            }
        }
        public static string GenerateRecycleItemsByJson()
        {
            try
            {
                List <Dictionary <string, string> > RecycleItemList = new List <Dictionary <string, string> >();

                foreach (ShellItem Item in RecycleBin.GetItems())
                {
                    try
                    {
                        Dictionary <string, string> PropertyDic = new Dictionary <string, string>
                        {
                            { "ActualPath", Item.FileSystemPath },
                            { "DeleteTime", (Item.IShellItem as Shell32.IShellItem2).GetFileTime(Ole32.PROPERTYKEY.System.Recycle.DateDeleted).ToInt64().ToString() }
                        };

                        if (File.Exists(Item.FileSystemPath))
                        {
                            PropertyDic.Add("StorageType", Enum.GetName(typeof(StorageItemTypes), StorageItemTypes.File));

                            if (Path.HasExtension(Item.Name))
                            {
                                PropertyDic.Add("OriginPath", Item.Name);
                            }
                            else
                            {
                                PropertyDic.Add("OriginPath", Item.Name + Item.FileInfo.Extension);
                            }
                        }
                        else if (Directory.Exists(Item.FileSystemPath))
                        {
                            PropertyDic.Add("OriginPath", Item.Name);
                            PropertyDic.Add("StorageType", Enum.GetName(typeof(StorageItemTypes), StorageItemTypes.Folder));
                        }
                        else
                        {
                            continue;
                        }

                        RecycleItemList.Add(PropertyDic);
                    }
                    finally
                    {
                        Item.Dispose();
                    }
                }

                return(JsonSerializer.Serialize(RecycleItemList));
            }
            catch
            {
                return(string.Empty);
            }
        }
        public static string GenerateRecycleItemsByJson()
        {
            try
            {
                List <Dictionary <string, string> > RecycleItemList = new List <Dictionary <string, string> >();

                foreach (ShellItem Item in RecycleBin.GetItems())
                {
                    try
                    {
                        Dictionary <string, string> PropertyDic = new Dictionary <string, string>
                        {
                            { "OriginPath", Item.IsLink ? $"{Item.Name}.lnk" : Item.Name },
                            { "ActualPath", Item.FileSystemPath }
                        };

                        if (Item.Properties.TryGetValue(Ole32.PROPERTYKEY.System.Recycle.DateDeleted, out object DeleteFileTime))
                        {
                            PropertyDic.Add("DeleteTime", Convert.ToString(((FILETIME)DeleteFileTime).ToInt64()));
                        }
                        else
                        {
                            PropertyDic.Add("DeleteTime", Convert.ToString(DateTimeOffset.MaxValue.ToFileTime()));
                        }

                        RecycleItemList.Add(PropertyDic);
                    }
                    finally
                    {
                        Item.Dispose();
                    }
                }

                return(JsonConvert.SerializeObject(RecycleItemList));
            }
            catch
            {
                return(string.Empty);
            }
        }
Exemple #5
0
        public static bool Restore(params string[] OriginPathList)
        {
            Dictionary <string, ShellItem> PathDic = new Dictionary <string, ShellItem>();

            try
            {
                foreach (ShellItem Item in RecycleBin.GetItems())
                {
                    if (File.Exists(Item.FileSystemPath))
                    {
                        if (Path.HasExtension(Item.Name))
                        {
                            PathDic.TryAdd(Item.Name, Item);
                        }
                        else
                        {
                            PathDic.TryAdd(Item.Name + Item.FileInfo.Extension, Item);
                        }
                    }
                    else if (Directory.Exists(Item.FileSystemPath))
                    {
                        PathDic.TryAdd(Item.Name, Item);
                    }
                }

                bool HasError = false;

                foreach (string OriginPath in OriginPathList)
                {
                    if (PathDic.TryGetValue(OriginPath, out ShellItem SourceItem))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(SourceItem.Name));

                        if (File.Exists(SourceItem.FileSystemPath))
                        {
                            File.Move(SourceItem.FileSystemPath, StorageController.GenerateUniquePath(OriginPath));
                        }
                        else if (Directory.Exists(SourceItem.FileSystemPath))
                        {
                            Directory.Move(SourceItem.FileSystemPath, StorageController.GenerateUniquePath(OriginPath));
                        }

                        string ExtraInfoPath = Path.Combine(Path.GetDirectoryName(SourceItem.FileSystemPath), Path.GetFileName(SourceItem.FileSystemPath).Replace("$R", "$I"));

                        if (File.Exists(ExtraInfoPath))
                        {
                            File.Delete(ExtraInfoPath);
                        }
                    }
                    else
                    {
                        HasError = true;
                    }
                }

                return(!HasError);
            }
            catch
            {
                return(false);
            }
            finally
            {
                foreach (ShellItem Item in PathDic.Values)
                {
                    Item.Dispose();
                }
            }
        }
Exemple #6
0
        public static string GenerateRecycleItemsByJson()
        {
            try
            {
                List <Dictionary <string, string> > RecycleItemList = new List <Dictionary <string, string> >();

                foreach (ShellItem Item in RecycleBin.GetItems())
                {
                    try
                    {
                        Dictionary <string, string> PropertyDic = new Dictionary <string, string>
                        {
                            { "ActualPath", Item.FileSystemPath }
                        };

                        try
                        {
                            PropertyDic.Add("DeleteTime", (Item.IShellItem as Shell32.IShellItem2).GetFileTime(Ole32.PROPERTYKEY.System.Recycle.DateDeleted).ToInt64().ToString());
                        }
                        catch
                        {
                            PropertyDic.Add("DeleteTime", default(FILETIME).ToInt64().ToString());
                        }

                        if (File.Exists(Item.FileSystemPath))
                        {
                            PropertyDic.Add("StorageType", Enum.GetName(typeof(StorageItemTypes), StorageItemTypes.File));

                            if (Path.GetExtension(Item.Name).Equals(Item.FileInfo.Extension, StringComparison.OrdinalIgnoreCase))
                            {
                                PropertyDic.Add("OriginPath", Item.Name);
                            }
                            else
                            {
                                PropertyDic.Add("OriginPath", Item.Name + Item.FileInfo.Extension);
                            }
                        }
                        else if (Directory.Exists(Item.FileSystemPath))
                        {
                            PropertyDic.Add("OriginPath", Item.Name);
                            PropertyDic.Add("StorageType", Enum.GetName(typeof(StorageItemTypes), StorageItemTypes.Folder));
                        }
                        else
                        {
                            continue;
                        }

                        RecycleItemList.Add(PropertyDic);
                    }
                    finally
                    {
                        Item.Dispose();
                    }
                }

                return(JsonSerializer.Serialize(RecycleItemList));
            }
            catch (Exception ex)
            {
                LogTracer.Log(ex, $"An exception was threw in {nameof(GenerateRecycleItemsByJson)}");
                return(string.Empty);
            }
        }