-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
329 lines (228 loc) · 10.2 KB
/
Form1.cs
File metadata and controls
329 lines (228 loc) · 10.2 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace Filewatcher
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//let's put this program in te upper right corner of the screen by default
this.StartPosition = FormStartPosition.Manual;
foreach (var scrn in Screen.AllScreens)
{
if (scrn.Bounds.Contains(this.Location))
{
this.Location = new Point(scrn.Bounds.Right - this.Width, scrn.Bounds.Top);
return;
}
}
}
// declare a publing string so we can store our changes and load them by default on startup
public string Watchpath; // for the watchpath
public string ScriptSource; // to link our script
// make a public bool so we can access settings after the correct password.
public static bool Admin = false;
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
MessageBox.Show("Closing this program is forbidden!" + "\n" + "\n" + "-MHG-", "Blocked on purpose", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
private void timer1_Tick(object sender, EventArgs e)
{
timer3.Stop(); // stop showing message that files are uploading , process is done.
label1.Text = "Monitoring Calibration Files";
pictureBox2.Visible = false;
pictureBox1.Visible = true;
}
private void Form1_Load(object sender, EventArgs e)
{
// read our last configurations back and load it @default
Watchpath = Filewatcher.Properties.Settings.Default.Watchpath;
ScriptSource = Filewatcher.Properties.Settings.Default.ScriptSource;
//let the FileWatcher know we want to read from our Public string Watchpath
fileSystemWatcher1.Path = Watchpath;
// let's show the path it is watching
textBox1.Text = Watchpath;
// let's show our default ScriptPath
textBox2.Text = ScriptSource;
// Sets the timer interval to 20seconds.
timer1.Interval = 10000; // for changes in Files
timer2.Interval = 30000; // to invoke our uploading powershell
timer3.Interval = 1000; //for our upload to show it is busy
ReloadForm();
//quick check to see if all is right.
}
private void change()
{
pictureBox1.Visible = false;
pictureBox2.Visible = true;
label1.Text = "Detected new Calibration Files";
timer1.Stop(); // stop first each time in case we are creating new files.
timer1.Start(); //show it's changing
timer2.Start(); // start to invoke our powershell after changes have been made.
}
private void upload()
{
timer1.Stop(); // stop showing processing
timer3.Start(); // show upload instead
}
private void BrowseButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
DialogResult result = MessageBox.Show("Do you want to change the monitoring directory to:" + "\n" + folderBrowserDialog1.SelectedPath, "Warning",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
Watchpath = folderBrowserDialog1.SelectedPath;
fileSystemWatcher1.Path = Watchpath;
Filewatcher.Properties.Settings.Default.Watchpath = Watchpath;
Filewatcher.Properties.Settings.Default.Save(); // Important for saving
textBox1.Text = Watchpath;
}
else if (result == DialogResult.No)
{
MessageBox.Show("Settings unchanged", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
/* /////////////////////////FILESYSTEMWATCHER-SETTINGS/////////////////////////
*/
private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
{
fileSystemWatcher1.EnableRaisingEvents = false;
LoadTreeViewData();
fileSystemWatcher1.EnableRaisingEvents = true;
change();
}
private void LoadTreeViewData()
{
throw new NotImplementedException();
}
private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
{
timer2.Stop(); // stop the powershell timer while we are still creating files
change(); // call our change
}
private void fileSystemWatcher1_Deleted(object sender, FileSystemEventArgs e)
{
//label2.Text = (string.Format("File Deleted {0} {1}", e.FullPath, e.Name));
label1.Text = "Ignoring Deleted files";
timer1.Start();
}
private void fileSystemWatcher1_Renamed(object sender, RenamedEventArgs e)
{
// label2.Text = (string.Format("File Renamed {0} {1}", e.FullPath, e.Name));
change();
}
private void SettingsButton_Click(object sender, EventArgs e)
{
Form_Login Form2 = new Form_Login(); // Instantiate a Form object.
Form2.ShowDialog();// Show Form2
ReloadForm(); // invoking reloadform setting Admin = true from form2 if password is correct.
}
private void ReloadForm()
{
if (Watchpath == "" || ScriptSource == "")
{
label1.Text = "No Config!| Click the Gears>> ";
pictureBox1.Visible = false;
pictureBox2.Visible = false;
pictureBox3.Visible = true;
}
if (Admin == true) // show our hidden things.
{
label1.Text = "Admin logged in";
Opacity = .95;
Size = new Size(351, 400);
pictureBox3.Visible = false;
AdminPic.Visible = true;
}
else if (Admin == false)
{
Opacity = .75;
Size = new Size(351, 108);
AdminPic.Visible = false;
//show our things
}
}
private void Terminal()
{
var script = ScriptSource;
//@"C:\scripts\myscript.ps1";
var startInfo = new ProcessStartInfo()
{
FileName = "powershell.exe",
Arguments = $"-windowstyle hidden -NoProfile -ExecutionPolicy unrestricted \"{script}\"",
UseShellExecute = false
};
upload();
timer2.Stop(); // we only want to execute this powershell once, hence stopping the ticking of the timer.
Process.Start(startInfo);
timer1.Start();
}
private void timer2_Tick(object sender, EventArgs e)
{
Terminal();
}
private void timer3_Tick(object sender, EventArgs e)
{
pictureBox1.Visible = false;
pictureBox2.Visible = true;
label1.Text = "Running UploadScript";
}
private void BrowseButton2_Click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Title = "Select A ScriptFile";
openfile.Filter = "scriptFiles (*.ps1)|*.ps1";
if (openfile.ShowDialog() == DialogResult.OK)
{
DialogResult result = MessageBox.Show("Do you want to change the Script File to:" + "\n" + openfile.FileName, "Warning",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
ScriptSource = openfile.FileName;// let's set our ScriptSource
Filewatcher.Properties.Settings.Default.ScriptSource = ScriptSource;
Filewatcher.Properties.Settings.Default.Save(); // Saves settings in application configuration file
textBox2.Text = ScriptSource;
}
else if (result == DialogResult.No)
{
MessageBox.Show("Settings unchanged", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
private void InfoButton_Click(object sender, EventArgs e)
{
MessageBox.Show("The copy source and destination must be set by the script you're invoking \n \n \nFor a script example, kindly see the pre-written script in the Program Files folder " , "Developers Info",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void button1_Click_1(object sender, EventArgs e)
{
Watchpath = "";
Filewatcher.Properties.Settings.Default.Watchpath = Watchpath;
Filewatcher.Properties.Settings.Default.Save(); // Important for saving
textBox1.Text = Watchpath;
ScriptSource = "";
Filewatcher.Properties.Settings.Default.ScriptSource = ScriptSource;
Filewatcher.Properties.Settings.Default.Save(); // Saves settings in application configuration file
textBox2.Text = ScriptSource;
}
private void ExitAdmin_Click(object sender, EventArgs e)
{
Admin = false;
pictureBox1.Visible = true;
pictureBox3.Visible = false;
label1.Text = "Monitoring Calibration Files";
ReloadForm(); // Size = new Size(351, 108);
}
}
}