async Task <String> fileWatch(WatchFileInfo watchFileInfo) { while (watchFileInfo.isWatching) { // 3秒経過 await Task.Delay(5000); System.IO.FileInfo fileInfo = new System.IO.FileInfo(watchFileInfo.FilePath); if (!fileInfo.Exists) { // 削除、ファイル名変更時 watchFileInfo.isExist = false; // 監視対象のファイルがなくなったため、背景色を灰色にするよう通知 NotExist = watchFileInfo.FilePath; } else { // 更新日時の更新 // 「ファイルの監視->ファイル名変更(背景灰色)->ファイル名を元に戻す」の操作の場合もここにきてファイル監視を再開する Exist = watchFileInfo.FilePath; } } return("finish"); }
/// <summary> /// ファイルの監視開始 /// </summary> /// <param name="addFileInfo">監視対象のファイル情報</param> public void watchStart(WatchFileInfo addWatchFileInfo) { // ファイル監視 Task <string> task = fileWatch(addWatchFileInfo); addWatchFileInfo.task = task; }
/// <summary> /// 「追加」ボタン押下時の処理。 /// </summary> private void AddFileExecute() { // ファイル選択ダイアログ var dlg = new MSAPI::Dialogs.CommonOpenFileDialog(); dlg.IsFolderPicker = false; dlg.Title = "監視するファイルを選択してください。"; dlg.InitialDirectory = @"C:"; if (dlg.ShowDialog() == MSAPI::Dialogs.CommonFileDialogResult.Ok) { // ファイルを選択したらココに来る WatchFileInfo fileInfo = new WatchFileInfo(dlg.FileName); // 重複チェック bool isExistFile = false; foreach (WatchFileInfo item in this.Items) { if (item.FilePath.Equals(fileInfo.FilePath)) { isExistFile = true; break; } } if (!isExistFile) { // DataGridに表示 Items.Add(fileInfo); // 監視開始 model.watchStart(fileInfo); } } }
public void DetectionModelPropertyChanged(object sender, PropertyChangedEventArgs args) { // 変更されたプロパティに対応するVMのプロパティに、変更値を反映 if (args.PropertyName == "Exist") { MainModel sendModel = (MainModel)sender; String changedFilePath = sendModel.Exist; ObservableCollection <WatchFileInfo> copyItems = new ObservableCollection <WatchFileInfo>(Items); foreach (WatchFileInfo item in copyItems) { if (item.FilePath.Equals(changedFilePath)) { int index = Items.IndexOf(item); copyItems.Remove(item); WatchFileInfo updataItem = new WatchFileInfo(item.FilePath); copyItems.Insert(index, updataItem); // 遷移先でRaisePropertyChanged("Itmes");を実施 Items = copyItems; break; } } } else if (args.PropertyName == "NotExist") { MainModel sendModel = (MainModel)sender; String deleteFilePath = sendModel.NotExist; ObservableCollection <WatchFileInfo> copyItems = new ObservableCollection <WatchFileInfo>(Items); foreach (WatchFileInfo item in copyItems) { if (item.FilePath.Equals(deleteFilePath)) { int index = Items.IndexOf(item); copyItems.Remove(item); WatchFileInfo updataItem = new WatchFileInfo(item.FilePath); copyItems.Insert(index, updataItem); // 遷移先でRaisePropertyChanged("Itmes");を実施 Items = copyItems; break; } } } }