public void UpdateEmptyRecycleBin()
        {
            Debug.WriteLine("UpdateEmptyRecycleBin");
            RecycleBinInfo info  = OS.GetAllRecycleBinInfo();
            string         label = "Empty Recycle Bins";

            if (info != null)
            {
                label += " (";
                if (info.ItemCount == 0 && info.Size == 0)
                {
                    label += "Empty";
                }
                else
                {
                    label += $"{info.ItemCount:N0} ";
                    if (info.ItemCount == 1)
                    {
                        label += $"Item";
                    }
                    else
                    {
                        label += $"Items";
                    }
                    label += $", {FormatBytes.Format(info.Size)}";
                }
                label += ")";
            }
            EmptyRecycleBinLabel = label;
            allRecycleBinInfo    = info;
            EmptyRecycleBin.RaiseCanExecuteChanged();
        }
예제 #2
0
        /* Methode zum Auslesen der Anzahl und der Größe der Dateien
         * im Papierkorb */
        public static RecycleBinInfo GetRecycleBinInfo(string rootPath)
        {
            // Die auszulesenden Laufwerke bestimmen
            ArrayList drives = new ArrayList();

            if (rootPath != null && rootPath != "")
            {
                drives.Add(rootPath);
            }
            else
            {
                // Alle Festplattenpartitionen ermitteln
                string[] logicalDrives = Environment.GetLogicalDrives();
                for (int i = 0; i < logicalDrives.Length; i++)
                {
                    if (GetDriveType(logicalDrives[i]) == DRIVE_FIXED)
                    {
                        drives.Add(logicalDrives[i]);
                    }
                }
            }

            // RecycleBinInfo-Instanz für das Ergebnis und SHQUERYRBINFO-Struktur
            // erzeugen
            RecycleBinInfo rbi           = new RecycleBinInfo();
            SHQUERYRBINFO  shQueryRBInfo = new SHQUERYRBINFO();

            // Größe der Struktur definieren, damit die API-Funktion diese beim
            // Schreiben kennt
            shQueryRBInfo.cbSize = Marshal.SizeOf(typeof(SHQUERYRBINFO));

            // Alle ermittelten Laufwerke durchgehen
            for (int i = 0; i < drives.Count; i++)
            {
                // SHQueryRecycleBin aufrufen und eventuelle Fehler auswerten
                int result = SHQueryRecycleBin(drives[i] + "\x0",
                                               ref shQueryRBInfo);
                if (result == 0)
                {
                    // Kein Fehler: Die gelesene Daten mit dem Ergebnis addieren
                    rbi.Size  += shQueryRBInfo.i64Size;
                    rbi.Count += shQueryRBInfo.i64NumItems;
                }
                else
                {
                    // Fehler beim Lesen: Ausnahme erzeugen
                    throw new Exception(String.Format(
                                            "Fehler 0x{0:X} beim Lesen des Papierkorbs", result));
                }
            }

            return(rbi);
        }
 //清空回收站
 private void ClearRecycle()
 {
     try
     {
         SetControlText(labelDetail, "开始清理系统垃圾......\r\n");
         RecycleBinInfo rInfo             = new RecycleBinInfo();
         long           long_RecycleSize  = 0;
         long           long_RecycleItems = 0;
         uint           result            = rInfo.QuerySizeRecycleBin(out long_RecycleSize, out long_RecycleItems);
         if (result == 0)
         {
             long_TotalSize  += long_RecycleSize;
             long_TotalItems += long_RecycleItems;
         }
         SetControlText(labelDetail, "正在清空回收站......\r\n");
         result = rInfo.EmptyRecycleBin(IntPtr.Zero, null, SHERB.SHERB_SILENT);
         SetControlText(labelTotalSize, ConvertSize(long_TotalSize));
     }
     catch
     {
     }
     #region
     //string[] disks = Environment.GetLogicalDrives();//获取硬盘上面的逻辑驱动器
     //foreach (string disk in disks)
     //{
     //    string path = disk + "";
     //    if (Directory.Exists(path))
     //    {
     //        try
     //        {
     //            deleteFiles(path, "*.*");
     //        }
     //        catch (Exception ex)
     //        {
     //            log.WriteLog(ex.ToString());
     //        }
     //        finally
     //        {
     //            SetControlText(labelDetail, "清空回收站完成!\r\n");
     //        }
     //    }
     //}
     #endregion
 }
예제 #4
0
        private void UpdateEmptyRecycleBinThread()
        {
            Debug.WriteLine("UpdateEmptyRecycleBin");
            RecycleBinInfo info  = OS.GetAllRecycleBinInfo();
            string         label = "Empty Recycle Bins";

            if (info != null)
            {
                label += " (";
                if (info.ItemCount == 0 && info.Size == 0)
                {
                    label += "Empty";
                }
                else
                {
                    label += $"{info.ItemCount:N0} ";
                    if (info.ItemCount == 1)
                    {
                        label += $"Item";
                    }
                    else
                    {
                        label += $"Items";
                    }
                    label += $", {FormatBytes.Format(info.Size)}";
                }
                label += ")";
            }
            EmptyRecycleBinLabel = label;
            allRecycleBinInfo    = info;
            EmptyRecycleBin.RaiseCanExecuteChanged();
            if (lastRecycleWatch == null)
            {
                lastRecycleWatch = Stopwatch.StartNew();
            }
            else
            {
                lastRecycleWatch.Restart();
            }
        }
        /// <summary>Gets the stats about every recycle bin.</summary>
        ///
        /// <returns>The info about the recycle bin on success, otherwise null.</returns>
        public RecycleBinInfo GetAllRecycleBinInfo()
        {
            //return ui.Invoke(() => {
            long itemCount = 0;
            long size      = 0;

            foreach (DriveInfo driveInfo in DriveInfo.GetDrives())
            {
                if (driveInfo.IsReady && driveInfo.DriveType != DriveType.Unknown &&
                    driveInfo.DriveType != DriveType.NoRootDirectory)
                {
                    RecycleBinInfo rbInfo = GetRecycleBinInfo(driveInfo.Name);
                    if (rbInfo != null)
                    {
                        itemCount += rbInfo.ItemCount;
                        size      += rbInfo.Size;
                    }
                }
            }
            return(new RecycleBinInfo(itemCount, size));
            //});
        }
 //清空回收站
 private void ClearRecycle()
 {
     try
     {
         SetControlText(labelDetail, "开始清理系统垃圾......\r\n");
         RecycleBinInfo rInfo = new RecycleBinInfo();
         long long_RecycleSize = 0;
         long long_RecycleItems = 0;
         uint result = rInfo.QuerySizeRecycleBin(out long_RecycleSize, out long_RecycleItems);
         if (result == 0)
         {
             long_TotalSize += long_RecycleSize;
             long_TotalItems += long_RecycleItems;
         }
         SetControlText(labelDetail, "正在清空回收站......\r\n");
         result = rInfo.EmptyRecycleBin(IntPtr.Zero, null, SHERB.SHERB_SILENT);
         SetControlText(labelTotalSize, ConvertSize(long_TotalSize));
     }
     catch
     {
     }
     #region
     //string[] disks = Environment.GetLogicalDrives();//获取硬盘上面的逻辑驱动器
     //foreach (string disk in disks)
     //{
     //    string path = disk + "";
     //    if (Directory.Exists(path))
     //    {
     //        try
     //        {
     //            deleteFiles(path, "*.*");
     //        }
     //        catch (Exception ex)
     //        {
     //            log.WriteLog(ex.ToString());
     //        }
     //        finally
     //        {
     //            SetControlText(labelDetail, "清空回收站完成!\r\n");
     //        }
     //    }
     //}
     #endregion
 }