-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormMain.cs
More file actions
172 lines (158 loc) · 6.59 KB
/
Copy pathFormMain.cs
File metadata and controls
172 lines (158 loc) · 6.59 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading.Tasks;
using System.Linq;
namespace BureauClean_Projet
{
public partial class Setup_BureauClean : Form
{
private StringBuilder string_log;
private bool log_present;
private FileSystemWatcher checker;
private bool check_on;
string path_desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
public Setup_BureauClean()
{
InitializeComponent();
versionToolStripMenuItem.Text = "Version : " + Application.ProductVersion;
string_log = new StringBuilder();
log_present = false;
check_on = false;
check();
}
public void check()
{
check_on = true;
btnCheckStatut.PerformClick();
btnCheckStatut.BackColor = Color.Red;
btnCheckStatut.Text = "Stop checking";
checker = new System.IO.FileSystemWatcher();
checker.Filter = "*.*";
checker.Path = path_desktop + "\\test\\";
checker.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
checker.Changed += new FileSystemEventHandler(OnChanged);
checker.Created += new FileSystemEventHandler(OnChanged);
checker.Deleted += new FileSystemEventHandler(OnChanged);
checker.Renamed += new RenamedEventHandler(OnRenamed);
checker.EnableRaisingEvents = true;
}
private void btnWatchFile_Click(object sender, EventArgs e)
{
if (check_on)
{
check_on = false;
checker.EnableRaisingEvents = false;
checker.Dispose();
btnCheckStatut.BackColor = Color.LightSkyBlue;
btnCheckStatut.Text = "Start checking";
}
else
{
check();
}
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
if (!log_present)
{
string_log.Remove(0, string_log.Length);
string_log.Append(e.FullPath);
string_log.Append("- is ");
string_log.Append(e.ChangeType.ToString());
string_log.Append("- le ");
string_log.Append(DateTime.Now.ToString());
if(e.ChangeType.ToString() == "Created")
{
System.Threading.Thread.Sleep(10000);
cleaning(e.FullPath, e.Name);
}
log_present = true;
}
}
static string getFolder_byext(string url_file)
{
var url_folder = new Dictionary<string, string>
{
//Images
[".png"] = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
[".gif"] = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
[".jpg"] = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
[".jpeg"] = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures),
//Documents textes
[".txt"] = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
[".doc"] = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
[".odt"] = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
[".docx"] = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
//Vidéos
[".avi"] = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos),
[".mp4"] = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos),
[".mkv"] = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos),
[".webm"] = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos),
//Musiques
[".mp3"] = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic),
};
string ext = Path.GetExtension(@url_file);
return url_folder[ext];
}
public void cleaning(string url_file, string name_file)
{
if (!File.Exists(url_file))
{
var directory = new DirectoryInfo(path_desktop + "\\test\\");
name_file = directory.GetFiles().OrderByDescending(f => f.LastWriteTime).First().Name;
url_file = directory.GetFiles().OrderByDescending(f => f.LastWriteTime).First().FullName;
}
string url_folder = getFolder_byext(url_file);
File.Move(url_file, url_folder + "\\" + name_file);
if (!log_present)
{
string_log.Remove(0, string_log.Length);
string_log.Append(name_file);
string_log.Append("- is ");
string_log.Append("Moved");
string_log.Append("- le ");
string_log.Append(DateTime.Now.ToString());
log_present = true;
}
}
private void OnRenamed(object sender, RenamedEventArgs e)
{
if (!log_present)
{
string_log.Remove(0, string_log.Length);
string_log.Append(e.OldFullPath);
string_log.Append("- is ");
string_log.Append(e.ChangeType.ToString());
string_log.Append(" ");
string_log.Append("à ");
string_log.Append(e.Name);
string_log.Append(" ");
string_log.Append(DateTime.Now.ToString());
log_present = true;
}
}
private void tmrEditNotify_Tick(object sender, EventArgs e)
{
if (log_present)
{
suiviBureau.BeginUpdate();
suiviBureau.Items.Add(string_log.ToString());
suiviBureau.EndUpdate();
log_present = false;
}
}
private void frmNotifier_Load(object sender, EventArgs e)
{
}
private void suiviBureau_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}