Skip to content

Commit 422bca1

Browse files
authored
Merge pull request #3 from d2phap/feature/v2
Feature/v2
2 parents 8b301bc + 663c86b commit 422bca1

29 files changed

Lines changed: 1096 additions & 1529 deletions

LICENSE

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
2-
Copyright (c) Microsoft Corporation
3-
4-
All rights reserved.
5-
61
MIT License
72

8-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
9-
files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy,
10-
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
11-
is furnished to do so, subject to the following conditions:
3+
Copyright (c) 2022 Duong Dieu Phap
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
1211

13-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
1414

15-
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16-
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
17-
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
18-
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,77 @@ A wrapper of C# `FileSystemWatcher` for Windows, used in [ImageGlass](https://gi
33

44
This project is based on the *VSCode FileWatcher*: https://github.com/Microsoft/vscode-filewatcher-windows
55

6-
6+
![Nuget](https://img.shields.io/nuget/dt/FileWatcherEx?color=%2300a8d6&logo=nuget)
77
[![Build status](https://ci.appveyor.com/api/projects/status/t20tf9qyta8enhu1?svg=true)](https://ci.appveyor.com/project/d2phap/filewatcherex)
88

9+
10+
## Resource links
11+
- Project url: [https://github.com/d2phap/FileWatcherEx](https://github.com/d2phap/FileWatcherEx)
12+
- Website: [https://imageglass.org](https://imageglass.org)
13+
- Nuget package: [https://www.nuget.org/packages/FileWatcherEx](https://www.nuget.org/packages/FileWatcherEx/)
14+
915
## Features
10-
- Standardize the events of C# `FileSystemWatcher`
16+
- Standardize the events of C# `FileSystemWatcher`.
1117
- No false change notifications when a file system item is created, deleted, changed or renamed.
18+
- Support .NET 6.0.
1219

1320
## Installation
1421
Run the command
1522
```bat
1623
Install-Package FileWatcherEx
1724
```
1825

26+
## Usage
27+
See Demo project for full details
28+
29+
```cs
30+
using FileWatcherEx;
31+
32+
33+
var _fw = new FileSystemWatcherEx(@"C:\path\to\watch");
34+
35+
// event handlers
36+
_fw.OnRenamed += FW_OnRenamed;
37+
_fw.OnCreated += FW_OnCreated;
38+
_fw.OnDeleted += FW_OnDeleted;
39+
_fw.OnChanged += FW_OnChanged;
40+
_fw.OnError += FW_OnError;
41+
42+
// thread-safe for event handlers
43+
_fw.SynchronizingObject = this;
44+
45+
// start watching
46+
_fw.Start();
47+
48+
49+
50+
void FW_OnRenamed(object sender, FileChangedEvent e)
51+
{
52+
// do something here
53+
}
54+
...
55+
56+
```
57+
1958
## License
2059
[MIT](LICENSE)
60+
61+
## Support this project
62+
63+
<a href="https://github.com/sponsors/d2phap" target="_blank" title="Become a sponsor">
64+
<img src="https://img.shields.io/badge/Github-@d2phap-24292e.svg?maxAge=3600&logo=github" height="30" alt="Become a sponsor">
65+
</a>
66+
67+
<a href="https://www.patreon.com/d2phap" target="_blank" title="Become a patron">
68+
<img src="https://img.shields.io/badge/Patreon-@d2phap%20-e85b46.svg?maxAge=3600&logo=patreon" height="30" alt="Become a patron">
69+
</a>
70+
71+
<a href="https://www.paypal.me/d2phap" target="_blank" title="Buy me a beer?">
72+
<img src="https://img.shields.io/badge/PayPal-Donate%20$10%20-0070ba.svg?maxAge=3600&logo=paypal" height="30" alt="Buy me a beer?">
73+
</a>
74+
75+
<a href="https://donorbox.org/imageglass" target="_blank" title="Wire transfer">
76+
<img src="https://img.shields.io/badge/DonorBox-@imageglass%20-005384.svg?maxAge=3600&logo=donorbox" height="30" alt="Wire transfer">
77+
</a>
78+
79+
Thanks for your gratitude and finance help!

Source/Demo/Demo.WinForms.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net6.0-windows</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<UseWindowsForms>true</UseWindowsForms>
8+
<ImplicitUsings>enable</ImplicitUsings>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\FileWatcherEx\FileWatcherEx.csproj" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 17 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/Demo/Form1.cs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
2+
using FileWatcherEx;
3+
4+
namespace Demo
5+
{
6+
public partial class Form1 : Form
7+
{
8+
private FileSystemWatcherEx _fw = new();
9+
10+
public Form1()
11+
{
12+
InitializeComponent();
13+
}
14+
15+
16+
17+
private void btnStart_Click(object sender, EventArgs e)
18+
{
19+
_fw = new FileSystemWatcherEx(txtPath.Text.Trim());
20+
21+
_fw.OnRenamed += FW_OnRenamed;
22+
_fw.OnCreated += FW_OnCreated;
23+
_fw.OnDeleted += FW_OnDeleted;
24+
_fw.OnChanged += FW_OnChanged;
25+
_fw.OnError += FW_OnError;
26+
27+
_fw.SynchronizingObject = this;
28+
29+
_fw.Start();
30+
31+
btnStart.Enabled = false;
32+
btnSelectFolder.Enabled = false;
33+
txtPath.Enabled = false;
34+
btnStop.Enabled = true;
35+
}
36+
37+
private void FW_OnError(object sender, ErrorEventArgs e)
38+
{
39+
if (txtConsole.InvokeRequired)
40+
{
41+
txtConsole.Invoke(FW_OnError, sender, e);
42+
}
43+
else
44+
{
45+
txtConsole.Text += "[ERROR]: " + e.GetException().Message + "\r\n";
46+
}
47+
}
48+
49+
private void FW_OnChanged(object sender, FileChangedEvent e)
50+
{
51+
txtConsole.Text += string.Format("[cha] {0} | {1}",
52+
Enum.GetName(typeof(ChangeType), e.ChangeType),
53+
e.FullPath) + "\r\n";
54+
}
55+
56+
private void FW_OnDeleted(object sender, FileChangedEvent e)
57+
{
58+
txtConsole.Text += string.Format("[del] {0} | {1}",
59+
Enum.GetName(typeof(ChangeType), e.ChangeType),
60+
e.FullPath) + "\r\n";
61+
}
62+
63+
private void FW_OnCreated(object sender, FileChangedEvent e)
64+
{
65+
txtConsole.Text += string.Format("[cre] {0} | {1}",
66+
Enum.GetName(typeof(ChangeType), e.ChangeType),
67+
e.FullPath) + "\r\n";
68+
}
69+
70+
private void FW_OnRenamed(object sender, FileChangedEvent e)
71+
{
72+
txtConsole.Text += string.Format("[ren] {0} | {1} ----> {2}",
73+
Enum.GetName(typeof(ChangeType), e.ChangeType),
74+
e.OldFullPath,
75+
e.FullPath) + "\r\n";
76+
}
77+
78+
79+
80+
private void btnStop_Click(object sender, EventArgs e)
81+
{
82+
_fw.Stop();
83+
84+
btnStart.Enabled = true;
85+
btnSelectFolder.Enabled = true;
86+
txtPath.Enabled = true;
87+
btnStop.Enabled = false;
88+
}
89+
90+
91+
private void btnSelectFolder_Click(object sender, EventArgs e)
92+
{
93+
var fb = new FolderBrowserDialog();
94+
95+
96+
if (fb.ShowDialog() == DialogResult.OK)
97+
{
98+
txtPath.Text = fb.SelectedPath;
99+
100+
_fw.Stop();
101+
_fw.Dispose();
102+
}
103+
}
104+
105+
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
106+
{
107+
_fw.Stop();
108+
_fw.Dispose();
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)