Ejemplo n.º 1
0
    /// <summary>
    /// The Delete
    /// </summary>
    /// <param name="days">The days<see cref="int"/></param>
    /// <param name="path">The path<see cref="string"/></param>
    public void Delete(int days, string path)
    {
        MainWindow.BGThread = (new Thread(() => {
            var Progress = new ProgressController();
            Progress.ShowProgressBar();

            try {
                if (!Directory.Exists(path))
                {
                    throw new ArgumentNullException("path not exists", nameof(path));
                }

                var dir = new DirectoryInfo(path);
                var FileList = dir.GetFiles();
                int iCnt = 0;

                foreach (var file in FileList)
                {
                    file.Refresh();
                    try {
                        if (file.LastAccessTime <= DateTime.Now.AddDays(-days))
                        {
                            Progress.AddLog("Deleting:" + file.Name);
                            file.Delete();
                            iCnt += 1;
                        }
                    }
                    catch (Exception) {
                        Progress.AddLog("Error");
                    }

                    Progress.AddProgress(1);
                }

                Progress.HideProgressBar();
            }
            catch (Exception ex) {
                Progress.AddLog(ex.StackTrace);
                Progress.HideProgressBar("!Error!");
            }
        }));

        MainWindow.BGThread.IsBackground = true;
        MainWindow.BGThread.Start();
    }
Ejemplo n.º 2
0
    /// <summary>
    /// The BackupFolder
    /// </summary>
    /// <param name="Name">The Name<see cref="string"/></param>
    /// <param name="path">The path<see cref="string[]"/></param>
    /// <param name="DeleteFile">The DeleteFile<see cref="bool"/></param>
    public void BackupFolder(string Name, string[] path, bool DeleteFile = true)
    {
        MainWindow.BGThread = (new Thread(() => {
            var Progress = new ProgressController();
            Progress.ShowProgressBar();
            try {
                if (!Directory.Exists(Anzu.Properties.Settings.Default.MainBackupFolder))
                {
                    System.Windows.MessageBox.Show("Backup folder not found on disk, set new folder in settings", "Error",
                                                   MessageBoxButton.OK, MessageBoxImage.Error);
                    throw new Exception("Backup folder not found on disk, set new folder in settings");
                }

                string zipPath = Anzu.Properties.Settings.Default.MainBackupFolder + Name + " " + DateTime.Now.ToString("dd.MM.yyyy (hh-mm)") + ".zip";
                Progress.AddLog("Backup to " + zipPath);

                var pathExists = path.Where(x => Directory.Exists(x));
                if (pathExists.Count() < 1)
                {
                    throw new ArgumentNullException("all path not exists", nameof(pathExists));
                }
                using (ZipFile zip = new ZipFile()) {
                    zip.CompressionLevel = GetComLvl();
                    zip.AlternateEncoding = Encoding.UTF8;
                    zip.AlternateEncodingUsage = ZipOption.AsNecessary;

                    zip.SaveProgress += (sender, e) => {
                        switch (e.EventType)
                        {
                        case ZipProgressEventType.Saving_AfterRenameTempArchive:
                            Progress.AddLog("Done");
                            Progress.AddLog("Archive size (byte):" + new FileInfo(e.ArchiveName).Length.ToString());
                            break;

                        case ZipProgressEventType.Saving_BeforeWriteEntry:
                            Progress.AddLog("Add:" + e.CurrentEntry.FileName);
                            break;

                        case ZipProgressEventType.Error_Saving:
                            Progress.AddLog("Error " + e.CurrentEntry);
                            DeleteFile = false;
                            break;

                        case ZipProgressEventType.Saving_AfterWriteEntry:
                            Progress.SetMax(e.EntriesTotal);
                            Progress.AddLog("Done:" + e.CurrentEntry.FileName);
                            Progress.SetText(e.EntriesSaved + "/" + e.EntriesTotal);
                            Progress.SetProgress(e.EntriesSaved);
                            break;
                        }
                    };
                    foreach (var p in pathExists)
                    {
                        zip.AddDirectory(p, "");
                    }
                    zip.Save(zipPath);
                }

                if (DeleteFile)
                {
                    foreach (var p in pathExists)
                    {
                        var FileList = new DirectoryInfo(p).GetFiles();
                        var DirectoryList = new DirectoryInfo(p).GetDirectories();

                        Progress.AddLog("///Start delete file///");
                        foreach (var current in FileList)
                        {
                            try {
                                Progress.AddLog(current.Name);
                                Progress.AddProgress(1);
                                if (current.FullName != zipPath)
                                {
                                    current.Delete();
                                }
                            }
                            catch (Exception ex) {
                                Progress.AddLog(ex.StackTrace);
                            }
                        }
                        foreach (var current in DirectoryList)
                        {
                            try {
                                Progress.AddLog(current.Name);
                                Progress.AddProgress(1);
                                current.Delete(true);
                            }
                            catch (Exception ex) {
                                Progress.AddLog(ex.StackTrace);
                            }
                        }
                    }
                }

                Progress.HideProgressBar();
            }
            catch (Exception ex) {
                Progress.AddLog(ex.StackTrace);
                Progress.HideProgressBar("!Error!");
            }
        }));

        MainWindow.BGThread.IsBackground = true;
        MainWindow.BGThread.Start();
    }
Ejemplo n.º 3
0
    /// <summary>
    /// The SortFolder
    /// </summary>
    /// <param name="DelFile">The DelFile<see cref="bool"/></param>
    /// <param name="SortExtension">The SortExtension<see cref="bool"/></param>
    /// <param name="path">The path<see cref="string"/></param>
    public void SortFolder(bool DelFile, bool SortExtension, string path)
    {
        MainWindow.BGThread = (new Thread(() => {
            var Progress = new ProgressController();
            Progress.ShowProgressBar();

            try {
                if (!Directory.Exists(path))
                {
                    throw new ArgumentNullException("path not exists", nameof(path));
                }

                var dir = new DirectoryInfo(path);
                var FileList = dir.GetFiles();
                Progress.SetMax(DelFile == true ? FileList.Length * 2 : FileList.Length);
                path += $"/Sort ({DateTime.Now.ToString("dd.MM.yyyy")})/";

                if (!SortExtension)
                {
                    foreach (var t in FileList)
                    {
                        try {
                            Progress.AddLog("Sort:" + t.Name);

                            Directory.CreateDirectory(path + TypeFiles.GetTypePath(t));
                            t.CopyTo(path + TypeFiles.GetTypePath(t) + t.Name, true);

                            Progress.AddProgress(1);
                        }
                        catch (Exception ex) {
                            Progress.AddLog("Error:" + t.Name);
                            Progress.AddLog(ex.StackTrace.ToString());
                            Progress.AddProgress(1);
                        }
                    }
                }
                else
                {
                    foreach (var t in FileList)
                    {
                        try {
                            Progress.AddLog("Sort:" + t.Name);
                            Directory.CreateDirectory(path + t.Extension.ToString().Replace(".", ""));
                            t.CopyTo(path + t.Extension.ToString().Replace(".", "") + "/" + t.Name, true);
                            Progress.AddProgress(1);
                        }
                        catch (Exception ex) {
                            Progress.AddLog("Error:" + t.Name);
                            Progress.AddLog(ex.StackTrace.ToString());
                            Progress.AddProgress(1);
                        }
                    }
                }

                if (DelFile)
                {
                    foreach (FileInfo file in dir.GetFiles())
                    {
                        try {
                            file.Delete();
                        }
                        catch (Exception ex) {
                            Progress.AddLog("Error:" + file.Name);
                            Progress.AddLog(ex.StackTrace.ToString());
                        }
                        finally {
                            Progress.AddProgress(1);
                        }
                    }
                }

                Progress.HideProgressBar();
            }
            catch (Exception ex) {
                Progress.AddLog(ex.StackTrace);
                Progress.HideProgressBar("!Error!");
            }
        }));

        MainWindow.BGThread.IsBackground = true;
        MainWindow.BGThread.Start();
    }