728x90
반응형
WPF & C# - FileSystemWatcher 파일시스템감시 ( 실시간 폴더 및 파일 변경사항 체크 ) |
MainWindow.xaml
1234567 <StackPanel><TextBox x:Name="txtChangeType"></TextBox><TextBox x:Name="txtFullPath"></TextBox><TextBox x:Name="txtName"></TextBox><TextBox x:Name="txtOldFullPath"></TextBox><TextBox x:Name="txtOldName"></TextBox></StackPanel>cs
MainWindow.xaml.cs
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 using System;using System.IO;using System.Windows;public MainWindow(){InitializeComponent();FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();fileSystemWatcher.Path = @"C:\Users\admin\Desktop"; // 감시 경로// fileSystemWatcher.Filter = "*.jpg"; // 파일 확장자fileSystemWatcher.EnableRaisingEvents = true; // 감시 시작여부fileSystemWatcher.Created += new System.IO.FileSystemEventHandler(fileSystemWatcher_Created);fileSystemWatcher.Changed += new System.IO.FileSystemEventHandler(fileSystemWatcher_Changed);fileSystemWatcher.Renamed += new System.IO.RenamedEventHandler(fileSystemWatcher_Renamed);fileSystemWatcher.Deleted += new System.IO.FileSystemEventHandler(fileSystemWatcher_Deleted);}// 파일생성시void fileSystemWatcher_Created(object sender, System.IO.FileSystemEventArgs e){this.Dispatcher.Invoke((Action)(() =>{writeText();txtChangeType.Text += e.ChangeType.ToString();txtFullPath.Text += e.FullPath;txtName.Text += e.Name;txtOldFullPath.Text = "";txtOldName.Text = "";}));}// 파일 변경void fileSystemWatcher_Changed(object sender, System.IO.FileSystemEventArgs e){this.Dispatcher.Invoke((Action)(() =>{writeText();txtChangeType.Text += e.ChangeType.ToString();txtFullPath.Text += e.FullPath;txtName.Text += e.Name;txtOldFullPath.Text = "";txtOldName.Text = "";}));}// 파일명 변경void fileSystemWatcher_Renamed(object sender, System.IO.RenamedEventArgs e){this.Dispatcher.Invoke((Action)(() =>{writeText();txtChangeType.Text += e.ChangeType.ToString();txtFullPath.Text += e.FullPath;txtName.Text += e.Name;txtOldFullPath.Text += e.OldFullPath;txtOldName.Text += e.OldName;}));}// 파일 삭제void fileSystemWatcher_Deleted(object sender, System.IO.FileSystemEventArgs e){this.Dispatcher.Invoke((Action)(() =>{writeText();txtChangeType.Text += e.ChangeType.ToString();txtFullPath.Text += e.FullPath;txtName.Text += e.Name;txtOldFullPath.Text += "";txtOldName.Text += "";}));}void writeText(){txtChangeType.Text = "ChangeType = ";txtFullPath.Text = "FullPath = ";txtName.Text = "Name = ";txtOldFullPath.Text = "OldFullPath = ";txtOldName.Text = "OldName = ";}cs
참고자료
https://www.c-sharpcorner.com/UploadFile/ad8d1c/watch-a-folder-for-updation-in-wpf-C-Sharp/
728x90
반응형