public WatchedFolderDialog(WatchedFolder folder)
        {
            InitializeComponent();
            for (int i = 0; i < 2; i++)
            {
                toolTips.Add(new ToolTip()
                {
                    InitialDelay = 0,
                    ShowAlways   = true,
                    UseAnimation = false,
                    UseFading    = false,
                });
            }

            textBoxWatched.TextChanged        += TextBoxPath_TextChanged;
            textBoxBackup.TextChanged         += TextBoxPath_TextChanged;
            comboBoxType.SelectedIndexChanged += ComboBoxType_SelectedIndexChanged;

            if (folder != null)
            {
                textBoxWatched.Text         = folder.Folder;
                comboBoxType.SelectedIndex  = (int)folder.StorageType;
                textBoxBackup.Text          = folder.BackupPath;
                numericUpDownInterval.Value = folder.Interval;
                numericUpDownSlots.Value    = folder.BackupSlots;
            }
            else
            {
                comboBoxType.SelectedIndex = 0;
            }
        }
 private void ButtonOK_Click(object sender, EventArgs e)
 {
     WatchedFolder = new WatchedFolder()
     {
         Folder      = textBoxWatched.Text,
         StorageType = (StorageType)comboBoxType.SelectedIndex,
         BackupPath  = textBoxBackup.Text,
         Interval    = Convert.ToInt32(numericUpDownInterval.Value),
         BackupSlots = Convert.ToInt32(numericUpDownSlots.Value)
     };
 }
示例#3
0
        private void Watcher_Alarm(object sender, FileSystemEventArgs e)
        {
            if (!e.Name.Contains(SUFFIX))
            {
                Log(e.FullPath + " - " + e.ChangeType);
                if (e.ChangeType != WatcherChangeTypes.Deleted)
                {
                    if (!Directory.Exists(tempDir))
                    {
                        Directory.CreateDirectory(tempDir);
                    }

                    string tempPath = Path.Combine(tempDir, e.Name + "_temp" + DateTime.Now.Ticks.ToString(CultureInfo.CurrentCulture));

                    try
                    {
                        WatchedFolder watchedFolder           = config.WatchedFolders[watchers.IndexOf(sender as FileSystemWatcher)];
                        DirectoryInfo backupDir               = new DirectoryInfo(watchedFolder.BackupPath);
                        IOrderedEnumerable <FileInfo> backups = backupDir.GetFiles(e.Name + SUFFIX + "*.zip").OrderBy(fi => fi.LastWriteTime);

                        if (!backups.Any() || (DateTime.Now - backups.Last().LastWriteTime).TotalSeconds >= watchedFolder.Interval)
                        {
                            string zipPath = backups.Count() < watchedFolder.BackupSlots
                                ? Path.Combine(backupDir.FullName, e.Name + GetVacantSuffix(backups, watchedFolder.BackupSlots))
                                : backups.First().FullName;

                            // is it a file?
                            if (File.Exists(e.FullPath))
                            {
                                using (FileStream stream = new FileStream(tempPath, FileMode.Create))
                                {
                                    using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create))
                                    {
                                        RetryIO(delegate() { archive.CreateEntryFromFile(e.FullPath, e.Name, CompressionLevel.Optimal); }, e.FullPath);
                                    }
                                }
                            }
                            else
                            {
                                RetryIO(delegate() { ZipFile.CreateFromDirectory(e.FullPath, tempPath, CompressionLevel.Optimal, true); }, e.FullPath);
                            }

                            if (File.Exists(zipPath))
                            {
                                File.Delete(zipPath);
                            }

                            File.Move(tempPath, zipPath);
                            Log("Successfully saved to " + zipPath);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log("ERROR: " + ex.Message);
                    }

                    try
                    {
                        if (File.Exists(tempPath))
                        {
                            File.Delete(tempPath);
                        }
                    }
                    catch
                    {
                        // do nothing really
                    }
                }
            }
        }