본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

WPF & C# - FileSystemWatcher 파일시스템감시 ( 실시간 폴더 및 파일 변경사항 체크 )

728x90
반응형
 WPF & C# - FileSystemWatcher 파일시스템감시 ( 실시간 폴더 및 파일 변경사항 체크 )

 

Test_fileSystemWatcher.zip
0.04MB

 

 

MainWindow.xaml

 

 
1
2
3
4
5
6
7
<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

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
반응형