/// <summary> /// Implementation of the Job Execute method. /// </summary> /// <param name="context">Reference to the JobExecutionContext.</param> public void Cleanup(string folder, int hours) { logger.Info("Executing FileCleanupJob"); try { logger.Info($"Checking Folder {folder}, for any files older than {hours} hours."); if (Directory.Exists(folder)) { // Handle files String[] fileNames = Directory.GetFiles(folder); foreach (string fileName in fileNames) { if (DateTime.Compare(DateTime.Now, File.GetCreationTime(fileName).AddHours(hours)) > 0) { logger.Debug($"Found file '{fileName}' older than {hours} hours, checking file permissions."); UserFileAccessRights userFileAccessRights = new UserFileAccessRights(folder); if (userFileAccessRights.canDelete()) { logger.Debug($"Verified permissions to delete file '{fileName}', attempting delete."); try { File.Delete(fileName); logger.Debug($"Successfully deleted file '{fileName}'."); } catch (Exception fileEx) { logger.Debug($"Exception occurred attempting to delete file '{fileName}'.", fileEx); } } else { logger.Debug($"Do not have permissions to delete file {fileName} in {folder}"); } } } } } catch (Exception ex) { logger.Error("Attempt to run FileCleanupJob failed with exception.", ex); } }
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { try { string str = (string)value; if (string.IsNullOrEmpty(str)) { return new ValidationResult(false, "Необходимо указать путь к выходному каталогу"); } str = str.Trim(); if (string.IsNullOrEmpty(str)) { return new ValidationResult(false, "Необходимо указать путь к выходному каталогу"); } DirectoryInfo di = new DirectoryInfo(str); if(!di.Exists) return new ValidationResult(false, "Не найден каталог"); if(di.GetDirectories().Length>0 || di.GetFiles().Length>0) return new ValidationResult(false, "Выбранный каталог должен быть пустым"); UserFileAccessRights rights = new UserFileAccessRights(str); if (!rights.canWrite() ) { return new ValidationResult(false, "У вас нет прав доступа на запись в каталог"); } if (rights.canRead()) { return new ValidationResult(true, "У вас нет прав доступа на чтение каталога"); } } catch (ArgumentException) { return new ValidationResult(false, "Не верно задан путь к каталогу"); } return new ValidationResult(true, null); }
private void addToListBtn_Click(object sender, EventArgs e) { string thePath = folderPathField.Text; if (thePath != String.Empty) { try { if (Directory.Exists(thePath)) { string theFolders = Program.GetSetting(Program.SettingType.folders); if (!theFolders.Contains(thePath)) { UserFileAccessRights rights = new UserFileAccessRights(thePath); if (rights.canDelete()) { string[] row = { thePath }; folderListView.Items.Add(new ListViewItem(row)); folderPathField.Text = ""; Program.SetSetting(Program.SettingType.folders, theFolders + thePath + ","); } else { MessageBox.Show("This software does not have permission to delete files in this folder. Try open as administrator, or pick a different folder."); } } else { MessageBox.Show("This folder has already been added"); } } else { MessageBox.Show("Folder doesn't exist"); } } catch { MessageBox.Show("Invalid folder path format"); } } else { MessageBox.Show("Please enter a path"); } }