diff --git a/Txgy.FilesWatcher.sln b/Txgy.FilesWatcher.sln new file mode 100644 index 0000000..1eae57b --- /dev/null +++ b/Txgy.FilesWatcher.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32811.315 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Txgy.FilesWatcher", "Txgy.FilesWatcher\Txgy.FilesWatcher.csproj", "{039EE0D8-DA93-4435-AB93-9F24802EC109}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {039EE0D8-DA93-4435-AB93-9F24802EC109}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {039EE0D8-DA93-4435-AB93-9F24802EC109}.Debug|Any CPU.Build.0 = Debug|Any CPU + {039EE0D8-DA93-4435-AB93-9F24802EC109}.Release|Any CPU.ActiveCfg = Release|Any CPU + {039EE0D8-DA93-4435-AB93-9F24802EC109}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8FDCAF0E-AEC9-448E-B672-71BD48DE8F7E} + EndGlobalSection +EndGlobal diff --git a/Txgy.FilesWatcher/App.xaml b/Txgy.FilesWatcher/App.xaml new file mode 100644 index 0000000..46d464f --- /dev/null +++ b/Txgy.FilesWatcher/App.xaml @@ -0,0 +1,14 @@ + + + + + + + + + + diff --git a/Txgy.FilesWatcher/App.xaml.cs b/Txgy.FilesWatcher/App.xaml.cs new file mode 100644 index 0000000..2421162 --- /dev/null +++ b/Txgy.FilesWatcher/App.xaml.cs @@ -0,0 +1,30 @@ +using Prism.Ioc; +using Prism.Regions; +using System.Windows; +using Txgy.FilesWatcher.Views; + +namespace Txgy.FilesWatcher +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App + { + protected override Window CreateShell() + { + return Container.Resolve(); + } + + protected override void RegisterTypes(IContainerRegistry containerRegistry) + { + containerRegistry.RegisterSingleton(); + + + + var iregion= Container.Resolve(); + iregion.RegisterViewWithRegion("MainContentRegion", typeof(MainView)); + } + + + } +} diff --git a/Txgy.FilesWatcher/Txgy.FilesWatcher.csproj b/Txgy.FilesWatcher/Txgy.FilesWatcher.csproj new file mode 100644 index 0000000..7f589b7 --- /dev/null +++ b/Txgy.FilesWatcher/Txgy.FilesWatcher.csproj @@ -0,0 +1,13 @@ + + + WinExe + net6.0-windows + true + True + + + + + + + \ No newline at end of file diff --git a/Txgy.FilesWatcher/ViewModels/MainViewModel.cs b/Txgy.FilesWatcher/ViewModels/MainViewModel.cs new file mode 100644 index 0000000..3ec039b --- /dev/null +++ b/Txgy.FilesWatcher/ViewModels/MainViewModel.cs @@ -0,0 +1,216 @@ +using DryIoc; +using Prism.Commands; +using Prism.Mvvm; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Controls; +using System.Windows; +using System.Collections.ObjectModel; +using System.Windows.Threading; + +namespace Txgy.FilesWatcher.ViewModels +{ + internal class MainViewModel:BindableBase + { + public MainViewModel() + { + + WatcherPath = "E:\\mingzf\\txgy\\WatchChanged"; + InitializeParams(); + } + + private string watcherPath; + + public string WatcherPath + { + get { return watcherPath; } + set { watcherPath = value; } + } + private List dataList=new List(); + + public List DataList + + { + get { return dataList; } + set { dataList = value; } + } + + private DateTime startTime; + public DateTime StartTime { get => startTime; set => SetProperty(ref startTime, value); } + private string runTime; + + public string RunTime + { + get { return runTime; } + set { SetProperty(ref runTime, value); } + } + + private ObservableCollection intervalTimesSource = new ObservableCollection(); + + public ObservableCollection IntervalTimesSource + { + get { return intervalTimesSource; } + set { intervalTimesSource = value; } + } + private int selectedIndex; + + public int SelectedIndex + { + get { return selectedIndex; } + set { SetProperty(ref selectedIndex, value); } + } + private int proMonInterval = 10; + public int ProMonInterval + { + get => proMonInterval; + set => SetProperty(ref proMonInterval, value); + } + private bool isIndeterminate = false; + + public bool IsIndeterminate + { + get { return isIndeterminate; } + set { SetProperty(ref isIndeterminate, value); } + } + public void InitializeParams() + { + timer1.Interval = TimeSpan.FromSeconds(ProMonInterval); + IntervalTimesSource.Add(5); + IntervalTimesSource.Add(10); + IntervalTimesSource.Add(15); + IntervalTimesSource.Add(20); + IntervalTimesSource.Add(30); + IntervalTimesSource.Add(60); + SelectedIndex = 1; + + watcher = new FileSystemWatcher(WatcherPath); + //初始化监听 + watcher.BeginInit(); + + //设置需要监听的更改类型(如:文件或者文件夹的属性,文件或者文件夹的创建时间;NotifyFilters枚举的内容) + watcher.NotifyFilter = NotifyFilters.Attributes + | NotifyFilters.CreationTime + | NotifyFilters.DirectoryName + | NotifyFilters.FileName + | NotifyFilters.LastAccess + | NotifyFilters.LastWrite + | NotifyFilters.Security + | NotifyFilters.Size; + //设置监听的路径 + watcher.Path = WatcherPath; + watcher.Changed += Watcher_Changed; + watcher.Created += Watcher_Created; + watcher.Deleted += Watcher_Deleted; + watcher.Renamed += Watcher_Renamed; + watcher.Error += OnError; + + //设置监听文件类型 + // watcher.Filter = "*.txt"; + //设置是否监听子目录 + watcher.IncludeSubdirectories = true; + //设置是否启用监听 + watcher.EnableRaisingEvents = false; + watcher.EndInit(); + + } + + public DelegateCommand StartCommand => new(Start); + + public DelegateCommand StopCommand => new(Stop); + + + private void Start() + { + WatchStartOrSopt(true); + } + + private void Stop() + { + WatchStartOrSopt(false); + } + + public DelegateCommand FilePathSaveCommand => new(() => + { + System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog(); + fbd.SelectedPath = WatcherPath; + fbd.ShowNewFolderButton = true; + if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK) + { + // MainModel.DataSavePath = fbd.SelectedPath; + //if (MainModel.DataSavePath != _systemConfig.vpnInfo.DataSavePath) + //{ + // updateJson = true; + //} + } + }); + + private void OnError(object sender, ErrorEventArgs e) + { + + } + + private void Watcher_Changed(object sender, FileSystemEventArgs e) + { + Application.Current.Dispatcher.BeginInvoke(new Action(() => { + + DateTime dt = DateTime.Now; + string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n"; + tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ; + tmp += "文件全称:" + e.FullPath + "\r\n"; + DataList.Add(tmp); + })); + } + + private void Watcher_Renamed(object sender, RenamedEventArgs e) + { + Application.Current.Dispatcher.BeginInvoke(new Action(() => { + + DateTime dt = DateTime.Now; + string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n"; + tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ; + tmp += "文件全称:" + e.FullPath + "\r\n"; + DataList.Add(tmp); + })); + } + + private void Watcher_Created(object sender, FileSystemEventArgs e) + { + Application.Current.Dispatcher.BeginInvoke(new Action(() => { + + DateTime dt = DateTime.Now; + string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n"; + tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ; + tmp += "文件全称:" + e.FullPath + "\r\n"; + DataList.Add(tmp); + })); + } + + private void Watcher_Deleted(object sender, FileSystemEventArgs e) + { + Application.Current.Dispatcher.BeginInvoke(new Action(() => { + + DateTime dt = DateTime.Now; + string tmp = dt.Hour.ToString() + "时" + dt.Minute.ToString() + "分" + dt.Second.ToString() + "秒" + dt.Millisecond.ToString() + "毫秒,目录发生变化\r\n"; + tmp += "改变类型 :" + e.ChangeType.ToString() + "\r\n"; ; + tmp += "文件全称:" + e.FullPath + "\r\n"; + DataList.Add(tmp); + })); + } + + /// + /// 启动或者停止监听 + /// + /// True:启用监听,False:关闭监听 + private void WatchStartOrSopt(bool IsEnableRaising) + { + watcher.EnableRaisingEvents = IsEnableRaising; + } + + private FileSystemWatcher watcher = new FileSystemWatcher(); + private DispatcherTimer timer1 = new DispatcherTimer(); + } +} diff --git a/Txgy.FilesWatcher/ViewModels/MainWindowViewModel.cs b/Txgy.FilesWatcher/ViewModels/MainWindowViewModel.cs new file mode 100644 index 0000000..feabae7 --- /dev/null +++ b/Txgy.FilesWatcher/ViewModels/MainWindowViewModel.cs @@ -0,0 +1,19 @@ +using Prism.Mvvm; + +namespace Txgy.FilesWatcher.ViewModels +{ + public class MainWindowViewModel : BindableBase + { + private string _title = "Application"; + public string Title + { + get { return _title; } + set { SetProperty(ref _title, value); } + } + + public MainWindowViewModel() + { + + } + } +} diff --git a/Txgy.FilesWatcher/Views/MainView.xaml b/Txgy.FilesWatcher/Views/MainView.xaml new file mode 100644 index 0000000..2a54a51 --- /dev/null +++ b/Txgy.FilesWatcher/Views/MainView.xaml @@ -0,0 +1,100 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +