Repository navigation
Expand file tree
/
Copy pathActions.cs
More file actions
1122 lines (1040 loc) · 51.6 KB
/
Copy pathActions.cs
File metadata and controls
1122 lines (1040 loc) · 51.6 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* AssistantComputerControl
* Made by Albert MN.
* Updated: v1.4.0, 15-01-2020
*
* Use:
* - Functions for all the actions
*/
using System;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;
namespace AssistantComputerControl {
class Actions {
public bool wasFatal = false;
public string successMessage, errorMessage;
//Logout
[DllImport("user32.dll", SetLastError = true)]
static extern int ExitWindowsEx(uint uFlags, uint dwReason);
//Lock
[DllImport("user32.dll")]
public static extern bool LockWorkStation();
//Turn off monitor
const int WM_SYSCOMMAND = 0x112;
const int SC_MONITORPOWER = 0xF170;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
//Music-control
public const int KEYEVENTF_EXTENTEDKEY = 1;
public const int KEYEVENTF_KEYUP = 0;
public const int VK_MEDIA_NEXT_TRACK = 0xB0; //Next track
public const int VK_MEDIA_PLAY_PAUSE = 0xB3; //Play/pause
public const int VK_MEDIA_PREV_TRACK = 0xB1; //Previous track
public const int VK_RCONTROL = 0xA3; //Right Control key code
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
//Key Shortcut
/*
* Keys and how to convert
* Each one on the left is accepted as a key inside the paramater
*/
String[,] charactersType = new String[,] {
{ "BACKSPACE", "{BACKSPACE}"},
{ "BREAK", "{BREAK}"},
{ "CAPS_LOCK", "{CAPSLOCK}"},
{ "DEL", "{DELETE}"},
{ "DELETE", "{DELETE}"},
{ "DOWN_ARROW", "{DOWN}"},
{ "END", "{END}"},
{ "ENTER", "{ENTER}"},
{ "ESC", "{ESC}"},
{ "HELP", "{HELP}"},
{ "HOME", "{HOME}"},
{ "INS", "{INS}"},
{ "INSERT", "{INSERT}"},
{ "LEFT_ARROW", "{LEFT}"},
{ "NUM_LOCK", "{NUMLOCK}"},
{ "PAGE_DOWN", "{PGDN}"},
{ "PAGE_UP", "{PGUP}"},
{ "PRINT_SCREEN", "{PRTSC}"},
{ "RIGHT_ARROW", "{RIGHT}"},
{ "SCROLL_LOCK", "{SCROLLLOCK}"},
{ "TAB", "{TAB}"},
{ "UP_ARROW", "{UP}"},
{ "F1", "{F1}"},
{ "F2", "{F2}"},
{ "F3", "{F3}"},
{ "F4", "{F4}"},
{ "F5", "{F5}"},
{ "F6", "{F6}"},
{ "F7", "{F7}"},
{ "F8", "{F8}"},
{ "F9", "{F9}"},
{ "F10", "{F10}"},
{ "F11", "{F11}"},
{ "F12", "{F12}"},
{ "F13", "{F13}"},
{ "F14", "{F14}"},
{ "F15", "{F15}"},
{ "F16", "{F16}"},
{ "Keypad_add", "{ADD}"},
{ "Keypad_subtract", "{SUBTRACT}"},
{ "Keypad_multiply", "{MULTIPLY}"},
{ "Keypad_divide", "{DIVIDE}"},
{ "SHIFT", "+"},
{ "CTRL", "^"},
{ "ALT", "%"}
};
//Baisc things
private void Error(string errorMsg, string debugMsg = null) {
MainProgram.DoDebug("ERROR: " + (String.IsNullOrEmpty(debugMsg) ? errorMsg : debugMsg));
//MainProgram.errorMessage = errorMsg;
errorMessage = errorMsg;
}
/*
* Actions
*/
public void Shutdown(string parameter) {
string shutdownParameters = "/s /t 0";
if (parameter != null) {
if (parameter == "abort") {
shutdownParameters = "abort";
} else if (parameter.Contains("/t") || parameter.Contains("-t")) {
shutdownParameters = (!parameter.Contains("/s") && !parameter.Contains("-s") ? "/s " : "") + parameter;
} else {
shutdownParameters = (!parameter.Contains("/s") && !parameter.Contains("-s") ? "/s " : "") + parameter + " /t 0";
}
}
if (MainProgram.testingAction) {
successMessage = "Simulated shutdown with parameters; " + shutdownParameters;
wasFatal = false;
} else {
if (shutdownParameters != "abort") {
MainProgram.DoDebug("Shutting down computer...");
successMessage = "Shutting down";
wasFatal = true;
Process.Start("shutdown", shutdownParameters);
} else {
MainProgram.DoDebug("Cancelling shutdown...");
wasFatal = false;
Process.Start("shutdown", "/a");
successMessage = "Aborted shutdown";
}
}
}
public void Restart(string parameter) {
string restartParameters = "/r /t 0";
if (parameter != null) {
if (parameter == "abort") {
restartParameters = "abort";
} else if (parameter.Contains("/t") || parameter.Contains("-t")) {
restartParameters = (!parameter.Contains("/r") && !parameter.Contains("-r") ? "/r " : "") + parameter;
} else {
restartParameters = (!parameter.Contains("/r") && !parameter.Contains("-r") ? "/r " : "") + parameter + " /t 0";
}
}
if (MainProgram.testingAction) {
successMessage = "Simulated restart";
} else {
if (restartParameters != "abort") {
MainProgram.DoDebug("Restarting computer...");
successMessage = "Restarting";
wasFatal = true;
Process.Start("shutdown", restartParameters);
} else {
MainProgram.DoDebug("Cancelling restart...");
wasFatal = true;
Process.Start("shutdown", "/a");
successMessage = "Aborted restart";
}
}
}
public void Sleep(string parameter) {
if (parameter == null) {
if (!MainProgram.testingAction) {
MainProgram.DoDebug("Sleeping computer...");
wasFatal = true;
Application.SetSuspendState(PowerState.Suspend, true, true);
}
} else {
bool doForce = true;
switch (parameter) {
case "true":
doForce = true;
break;
case "false":
doForce = false;
break;
default:
Error("Parameter \"" + parameter + "\" is invalid for the \"sleep\" action. Accepted parameters: \"true\" and \"false\")");
break;
}
if (!MainProgram.testingAction) {
MainProgram.DoDebug("Sleeping computer...");
wasFatal = true;
Application.SetSuspendState(PowerState.Suspend, doForce, true);
}
}
if (MainProgram.testingAction) {
successMessage = "Simulated PC sleep";
} else {
successMessage = "Put computer to sleep";
}
}
public void Hibernate(string parameter) {
if (parameter == null) {
if (!MainProgram.testingAction) {
MainProgram.DoDebug("Hibernating computer...");
wasFatal = true;
Application.SetSuspendState(PowerState.Hibernate, true, true);
}
} else {
bool doForce = true;
switch (parameter) {
case "true":
doForce = true;
break;
case "false":
doForce = false;
break;
default:
Error("Parameter \"" + parameter + "\" is invalid for the \"hibernate\" action. Accepted parameters: \"true\" and \"false\")");
break;
}
if (!MainProgram.testingAction) {
MainProgram.DoDebug("Hibernating computer...");
wasFatal = true;
Application.SetSuspendState(PowerState.Hibernate, doForce, true);
}
}
if (MainProgram.testingAction) {
successMessage = "Simulated PC hibernate";
} else {
successMessage = "Put computer in hibernation";
}
}
public void Logout(string parameter) {
if (MainProgram.testingAction) {
successMessage = "Simulated logout";
} else {
MainProgram.DoDebug("Logging out of user...");
successMessage = "Logged out of user";
wasFatal = true;
ExitWindowsEx(0, 0);
}
}
public void Lock(string parameter) {
if (MainProgram.testingAction) {
successMessage = "Simulated PC lock";
} else {
MainProgram.DoDebug("Locking computer...");
wasFatal = true;
LockWorkStation();
successMessage = "Locked pc";
}
}
public void Mute(string parameter) {
bool doMute = true;
if (parameter == null) {
//No parameter - toggle
try {
doMute = !AudioManager.GetMasterVolumeMute();
} catch {
Error("Failed to mute; no volume object.");
}
} else {
//Parameter set;
switch (parameter) {
case "true":
doMute = true;
break;
case "false":
doMute = false;
break;
default:
Error("Parameter \"" + parameter + "\" is invalid for the \"mute\" action. Accepted parameters: \"true\" and \"false\")");
break;
}
}
if (MainProgram.testingAction) {
successMessage = "Simulated PC" + (doMute ? "muted " : "unmute");
} else {
try {
//Sometimes fails - sentry @833243007
AudioManager.SetMasterVolumeMute(doMute);
} catch {
Error("Failed to set PC mute status", "Failed to set PC mute static. Exception caught.");
}
successMessage = (doMute ? "Muted " : "Unmuted") + " pc";
}
}
public void SetVolume(string parameter) {
bool numberIsGood = double.TryParse(parameter, out double volumeLevel);
if (!numberIsGood) {
double newNum = new WordsToNumbers().EnglishGet(parameter);
numberIsGood = (newNum != -1);
volumeLevel = newNum;
}
if (numberIsGood) {
if (volumeLevel >= 0 && volumeLevel <= 100) {
if (!MainProgram.testingAction) {
if (Properties.Settings.Default.UnmuteOnVolumeChange) {
try {
//Sometimes fails - sentry @833243007
AudioManager.SetMasterVolumeMute(false);
} catch {
Error("Failed to unmute PC", "Failed to unmute PC. Exception caught.");
}
}
try {
AudioManager.SetMasterVolume((float)volumeLevel);
} catch {
//Might not have an audio device...
Error("Failed to set PC volume", "Failed to set PC volume. Exception caught.");
}
}
if (!MainProgram.testingAction) {
try {
/*if ((int)AudioManager.GetMasterVolume() != (int)volumeLevel) {
//Something went wrong... Audio not set to parameter-level
MainProgram.DoDebug("ERROR: Volume was not set properly. Master volume is " + AudioManager.GetMasterVolume() + ", not " + volumeLevel);
MainProgram.errorMessage = "Something went wrong when setting the volume";
} else {
successMessage = "Set volume to " + volumeLevel + "%";
}*/
//Don't check - have faith. The check causes trouble (removed in v1.2.4)
successMessage = "Set volume to " + volumeLevel + "%";
} catch {
Error("Failed to check volume");
}
} else {
successMessage = "Simulated setting system volume to " + volumeLevel + "%";
}
} else {
Error("Can't set volume to " + volumeLevel + "%, has to be a number from 0-100");
}
} else {
Error("Not a valid parameter (has to be a number)", "Parameter (" + parameter + ") not convertable to double");
}
}
public void Music(string parameter) {
switch (parameter) {
case "previous":
if (MainProgram.testingAction) {
successMessage = "MUSIC: Simulated going to previous track";
} else {
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, 0);
successMessage = "MUSIC: Skipped song";
}
break;
case "previousx2":
if (MainProgram.testingAction) {
successMessage = "MUSIC: Simulated double-previous track";
} else {
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, 0);
Thread.Sleep(100);
keybd_event(VK_MEDIA_PREV_TRACK, 0, KEYEVENTF_EXTENTEDKEY, 0);
successMessage = "MUSIC: Skipped song (x2)";
}
break;
case "next":
if (MainProgram.testingAction) {
successMessage = "MUSIC: Simulated going to next song";
} else {
keybd_event(VK_MEDIA_NEXT_TRACK, 0, KEYEVENTF_EXTENTEDKEY, 0);
successMessage = "MUSIC: Next song";
}
break;
case "play_pause":
if (MainProgram.testingAction) {
successMessage = "MUSIC: Simulated play/pause";
} else {
keybd_event(VK_MEDIA_PLAY_PAUSE, 0, KEYEVENTF_EXTENTEDKEY, 0);
successMessage = "MUSIC: Played/Paused";
}
break;
default:
Error("Unknown parameter \"" + parameter + "\"");
break;
}
}
public void Open(string parameter) {
string location = ActionChecker.GetSecondaryParam(parameter)[0], arguments = (ActionChecker.GetSecondaryParam(parameter).Length > 1 ? ActionChecker.GetSecondaryParam(parameter)[1] : null);
string fileLocation = (!location.Contains(@":\") || !location.Contains(@":/")) ? "" : location;
if (fileLocation == "") {
string combinedPath = "";
try {
combinedPath = Path.Combine(MainProgram.shortcutLocation, location);
//MainProgram.DoDebug();
} catch {
Error("Given path (" + location+ ") is invalid (could not combine)");
}
if (combinedPath != "") {
fileLocation = combinedPath;
}
}
if (fileLocation != "") {
MainProgram.DoDebug(fileLocation);
try {
if (File.Exists(fileLocation) || Directory.Exists(fileLocation) || Uri.IsWellFormedUriString(fileLocation, UriKind.Absolute)) {
if (!MainProgram.testingAction) {
try {
Process p = new Process();
p.StartInfo.FileName = fileLocation;
if (arguments != null)
p.StartInfo.Arguments = arguments;
p.Start();
successMessage = "OPEN: opened file/url; " + fileLocation;
} catch (Exception e) {
MainProgram.DoDebug("Failed to open file; " + e.Message);
Error("Failed to open file (" + fileLocation + ")");
}
} else {
successMessage = "OPEN: simulated opening file; " + fileLocation;
}
} else {
Error("File or directory doesn't exist (" + fileLocation + ")");
}
} catch {
MainProgram.DoDebug("Error when opening file");
}
}
}
public void OpenAll(string parameter) {
string fileLocation = (!parameter.Contains(@":\") || !parameter.Contains(@":/")) ? Path.Combine(MainProgram.shortcutLocation, parameter) : parameter;
if (Directory.Exists(fileLocation) || Uri.IsWellFormedUriString(fileLocation, UriKind.Absolute)) {
DirectoryInfo d = new DirectoryInfo(fileLocation);
int x = 0;
foreach (var dirFile in d.GetFiles()) {
if (!MainProgram.testingAction)
Process.Start(dirFile.FullName);
x++;
}
if (!MainProgram.testingAction) {
successMessage = "OPEN: opened " + x + " files in; " + fileLocation;
} else {
successMessage = "OPEN: simulated opening " + x + " files in; " + fileLocation;
}
} else {
Error("Directory doesn't exist (" + fileLocation + ")");
}
}
public void Die(string parameter) {
if (!MainProgram.testingAction) {
successMessage = "Shutting down ACC";
Application.Exit();
} else {
successMessage = "Simulated shutting down ACC";
}
}
public void MonitorsOff(string parameter) {
if (!MainProgram.testingAction) {
Form f = new Form();
SendMessage(f.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)2);
f.Close();
successMessage = "Turned monitors off";
} else {
successMessage = "Simulated turning monitors off";
}
}
private static void PressKey(char c) {
//TODO
try {
SendKeys.SendWait(c.ToString());
} catch (Exception e) {
MainProgram.DoDebug("Failed to press key \"" + c.ToString() + "\", exception; " + e);
}
}
public void WriteOut(string parameter, string line) {
int i = 0;
string writtenString = "";
foreach (char c in parameter) {
char toWrite = (i == 0 && Properties.Settings.Default.WriteOutUCFirst ? Char.ToUpper(c) : c);
if (!MainProgram.testingAction) PressKey(toWrite);
writtenString += toWrite;
if (i > line.Length && Properties.Settings.Default.WriteOutDotLast) {
if (!MainProgram.testingAction) PressKey('.');
writtenString += ".";
}
i++;
}
if (!MainProgram.testingAction) {
successMessage = "Wrote \"" + writtenString + "\"";
} else {
successMessage = "Simulated writing \"" + writtenString + "\"";
}
}
public void KeyShortcut(string parameter) {
/*
* Added by: Joshua Miller
* How to use it:
* - To seperate keys please use '+' (to use '+' do {ADD})
* - Things like ctrl will be converted to control key
*/
// Split up commands
char splitChar = '+';
String[] keyCombinationInput = parameter.Split(splitChar);
// Will be added onto to make what to type
String keyCombinationPress = "";
// Put commands into correct form
for (int index = 0; index < keyCombinationInput.Length; index++) {
// Get current command
String command = keyCombinationInput[index];
// If not empty
if (command != "") {
// If one character (not command)
if (command.Length == 1) {
// Add to the out
keyCombinationPress = keyCombinationPress + command.ToLower();
} else {
// If it is a command (probably)
// Check if it is a possible command and needs to be changed
bool foundYet = false;
for (int countInCharacterArray = 0; countInCharacterArray < charactersType.GetLength(0) && foundYet == false; countInCharacterArray++) {
String characterTestNow = charactersType[countInCharacterArray, 0];
if (Equals(command.ToUpper(), characterTestNow)) {
keyCombinationPress += charactersType[countInCharacterArray, 1];
foundYet = true;
} else if (Equals(command.ToUpper(), charactersType[countInCharacterArray, 1])) {
keyCombinationPress += charactersType[countInCharacterArray, 1];
foundYet = true;
}
}
if (foundYet == false) {
MainProgram.DoDebug("KeyShortcut Action - Warning: A command " + command.ToUpper() + " was not identified, please be weary as this may not work");
MainProgram.DoDebug("KeyShortcut Action - Warning: Adding Anyway");
keyCombinationPress += command;
}
}
} else {
MainProgram.DoDebug("KeyShortcut Action - Warning: A character inside the paramater was blank");
}
}
// Is it testing?
if (MainProgram.testingAction) {
successMessage = ("Simulated sending the combination: " + keyCombinationPress);
} else {
// Try pressing keys
bool keysPressedSuccess = true;
try {
SendKeys.SendWait(keyCombinationPress);
} catch (ArgumentException) {
Error("Key combination is not valid");
keysPressedSuccess = false;
}
if (keysPressedSuccess) {
successMessage = ("Sending the combination: " + keyCombinationPress);
}
}
}
public void CreateFile(string parameter) {
string fileLocation = parameter;
if (!File.Exists(fileLocation)) {
string parentPath = Path.GetDirectoryName(fileLocation);
if (Directory.Exists(parentPath)) {
bool succeeded = true;
try {
string toDelete;
//Is file
if (MainProgram.testingAction) {
//Create a test-file and delete it to test if has permission
toDelete = Path.Combine(parentPath, "acc_testfile.txt");
var myFile = File.Create(toDelete);
myFile.Close();
while (!File.Exists(toDelete)) ;
while (!ActionChecker.FileInUse(toDelete)) ;
File.Delete(toDelete);
} else {
//Actually create file
var myFile = File.Create(fileLocation);
myFile.Close();
}
} catch (Exception exc) {
succeeded = false;
Error("Couldn't create file - folder might be locked. Try running ACC as administrator.", exc.Message);
}
if (succeeded) {
if (!MainProgram.testingAction) {
successMessage = "Created file at " + fileLocation;
} else {
successMessage = "Simulated creating file at " + fileLocation;
}
}
} else {
Error("File parent folder doesn't exist (" + parentPath + ")");
}
} else {
Error("File already exists");
}
}
public void DeleteFile(string parameter) {
string fileLocation = parameter;
if (File.Exists(fileLocation) || Directory.Exists(fileLocation)) {
FileAttributes attr = File.GetAttributes(fileLocation);
bool succeeded = true;
MainProgram.DoDebug("Deleting file/folder at " + fileLocation);
try {
string toDelete;
if (attr.HasFlag(FileAttributes.Directory)) {
//Is folder
MainProgram.DoDebug("Deleting folder...");
DirectoryInfo d = new DirectoryInfo(fileLocation);
bool doDelete = true;
if (d.GetFiles().Length > Properties.Settings.Default.MaxDeleteFiles && Properties.Settings.Default.WarnWhenDeletingManyFiles) {
//Has more than x files - do warning
DialogResult dialogResult = MessageBox.Show("You're about to delete more than " + Properties.Settings.Default.MaxDeleteFiles.ToString() + " files at " + fileLocation + " - are you sure you wish to proceed?",
"Are you sure?", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes) {
} else if (dialogResult == DialogResult.No) {
doDelete = false;
}
}
if (doDelete) {
if (MainProgram.testingAction) {
//Make test-folder and delete it to test if has permission
toDelete = Path.Combine(Directory.GetParent(fileLocation).FullName, "acc_testfolder");
Directory.CreateDirectory(toDelete);
Directory.Delete(toDelete);
} else {
//Actually delete folder
Directory.Delete(fileLocation);
MainProgram.DoDebug("Deleted directory at " + fileLocation);
}
}
} else {
//Is file
if (MainProgram.testingAction) {
//Make test-file and delete it to test if has permission
MainProgram.DoDebug("(Fake) Deleting file...");
toDelete = Path.Combine(fileLocation, "acc_testfile.txt");
File.Create(toDelete);
File.Delete(toDelete);
} else {
//Actually delete file
MainProgram.DoDebug("Deleting file...");
File.Delete(fileLocation);
}
}
} catch (Exception exc) {
succeeded = false;
Error("Couldn't access file/folder - file might be in use or locked. Try running ACC as administrator.", exc.Message);
}
if (succeeded) {
if (!MainProgram.testingAction) {
successMessage = "Deleted file/folder at " + fileLocation;
} else {
successMessage = "Simulated deleting file/folder at " + fileLocation;
}
}
} else {
Error("File or folder doesn't exist");
}
}
public void AppendText(string parameter) {
string fileLocation = ActionChecker.GetSecondaryParam(parameter)[0],
toAppend = ActionChecker.GetSecondaryParam(parameter).Length > 1 ? ActionChecker.GetSecondaryParam(parameter)[1] : null;
MainProgram.DoDebug("Appending \"" + toAppend + "\" to " + fileLocation);
if (fileLocation != null && toAppend != null) {
if (toAppend != "") {
if (File.Exists(fileLocation)) {
string parentPath = Path.GetDirectoryName(fileLocation);
bool succeeded = true;
try {
//Is file
if (MainProgram.testingAction) {
//Write empty string to file to test permission
using (StreamWriter w = File.AppendText(fileLocation)) {
w.Write(String.Empty);
}
} else {
//Actually write to file
using (StreamWriter w = File.AppendText(fileLocation)) {
//string[] lines = toAppend.Split(new string[] { "\n" }, StringSplitOptions.None);
string[] lines = toAppend.Split(new string[] { "\\n" }, StringSplitOptions.None);
MainProgram.DoDebug(lines.Length.ToString());
int i = 0;
foreach (string appendChild in lines) {
if (i == 0)
w.Write(appendChild);
else {
w.WriteLine(String.Empty);
w.Write(appendChild);
}
i++;
}
}
}
} catch (Exception exc) {
succeeded = false;
Error("Couldn't create file - folder might be locked. Try running ACC as administrator.", exc.Message);
}
if (succeeded) {
if (!MainProgram.testingAction) {
successMessage = "Appended \"" + toAppend + "\" to file at " + fileLocation;
} else {
successMessage = "Simulated appending \"" + toAppend + "\" to file at " + fileLocation;
}
}
} else {
Error("File doesn't exists");
}
} else {
Error("Can't append nothing");
}
} else {
Error("Parameter doesn't contain a string to append");
}
}
public void DoMessageBox(string parameter) {
string theMessage = ActionChecker.GetSecondaryParam(parameter)[0]
, theTitle = (ActionChecker.GetSecondaryParam(parameter).Length > 1 ? ActionChecker.GetSecondaryParam(parameter)[1] : null);
MainProgram.DoDebug("TESTTTTT ::::: " + (theTitle == null).ToString());
if (MainProgram.testingAction) {
successMessage = "Simulated making a message box with the content \"" + theMessage + "\" and " + (String.IsNullOrEmpty(theTitle) ? "no title" : "custom title \"" + theTitle + "\"");
} else {
new Thread(() => {
Thread.CurrentThread.Priority = ThreadPriority.Highest;
MessageBox.Show(theMessage, String.IsNullOrEmpty(theTitle) ? "ACC Generated Message Box" : theTitle);
}).Start();
}
}
public void KillProcess(string parameter) {
bool paramIsNum = int.TryParse(parameter, out int pid);
if (paramIsNum) {
try {
//Results in an exception if process doesn't exist
Process theP = Process.GetProcessById(pid);
if (!MainProgram.testingAction) {
try {
//theP.Close();
theP.Kill();
successMessage = "Killed process with ID " + pid.ToString();
} catch (Exception e) {
Error("Failed to kill process with ID " + pid.ToString() + "; " + e.Message);
}
} else {
successMessage = "Successfully simulated killing process";
}
} catch {
Error("A process with the ID " + pid.ToString() + " doesn't exist");
}
} else {
try {
//Results in an exception if process doesn't exist
Process[] thePs = Process.GetProcessesByName(parameter);
if (!MainProgram.testingAction) {
try {
foreach (Process p in thePs) {
p.Kill();
}
successMessage = "Killed all processes with name " + parameter;
} catch (Exception e) {
Error("Failed to kill processes with name " + parameter + "; " + e.Message);
}
} else {
successMessage = "Successfully simulated killing processes";
}
} catch {
Error("A process with the name " + parameter + " doesn't exist");
}
}
}
public void MoveSubject(string parameter) {
string theSubject = ActionChecker.GetSecondaryParam(parameter)[0],
moveTo = (ActionChecker.GetSecondaryParam(parameter).Length > 1 ? ActionChecker.GetSecondaryParam(parameter)[1] : null);
FileAttributes attr = FileAttributes.Normal; //Has to have a default value
bool subjectExists = true;
try {
attr = File.GetAttributes(theSubject);
} catch {
subjectExists = false;
}
if (subjectExists) {
if (attr.HasFlag(FileAttributes.Directory)) {
//Is directory
bool isPath = true;
try {
Path.GetFullPath(moveTo);
} catch {
isPath = false;
}
if (isPath) {
if (Directory.Exists(theSubject)) {
if (Directory.Exists(moveTo)) {
//Dest is a folder
string newMoveTo = Path.Combine(moveTo, Path.GetFileName(new DirectoryInfo(theSubject).Name));
if (MainProgram.testingAction) {
successMessage = "Simulated moving folder from \"" + theSubject + "\" to \"" + newMoveTo + "\"";
} else {
try {
Directory.Move(theSubject, newMoveTo);
successMessage = "Moved folder from \"" + theSubject + "\" to \"" + newMoveTo + "\"";
} catch (Exception e) {
Error("[Move action] Couldn't move folder (" + theSubject + ") to path; '" + newMoveTo + "'. Got error; " + e.Message);
}
}
} else {
//Dest is a new folder name!
if (MainProgram.testingAction) {
successMessage = "Simulated moving (renaming) folder from \"" + theSubject + "\" to \"" + moveTo + "\"";
} else {
try {
Directory.Move(theSubject, moveTo);
successMessage = "Moved (renamed) folder from \"" + theSubject + "\" to \"" + moveTo + "\"";
} catch (Exception e) {
Error("[Move action] Couldn't move folder (" + theSubject + "); " + e.Message);
}
}
}
} else {
Error("[Move action] Subject folder doesn't exist");
}
} else {
Error("[Move action] To move a folder, the destination (secondary parameter) must be the path to a folder");
}
} else {
//Is file
if (File.Exists(theSubject)) {
if (File.Exists(moveTo) || Directory.Exists(moveTo)) {
attr = File.GetAttributes(moveTo);
if (attr.HasFlag(FileAttributes.Directory)) {
//Desination is a folder
if (Directory.Exists(moveTo)) {
var theDestination = Path.Combine(moveTo, Path.GetFileName(theSubject));
if (MainProgram.testingAction) {
successMessage = "Simulated moving file from \"" + moveTo + "\" to \"" + theDestination + "\"";
} else {
try {
File.Move(theSubject, theDestination);
successMessage = "Moved file from \"" + moveTo + "\" to \"" + theDestination + "\"";
} catch (Exception e) {
Error("[Move action] Couldn't move file to folder; " + e.Message);
}
}
} else {
Error("[Move action] Desination folder doesn't exist");
}
} else {
//Desination is a file - either overwrite existing file, or "rename" file to dest
try {
File.Move(theSubject, moveTo);
} catch (Exception e) {
Error("[Move action] Couldn't move file; " + e.Message);
}
}
} else {
try {
File.Move(theSubject, moveTo);
} catch (Exception e) {
Error("[Move action] Couldn't move file; " + e.Message);
}
}
} else {
Error("[Move action] Subject file doesn't exist");
}
}
} else {
Error("[Move action] Subject (folder or file) doesn't exist");
}
if (MainProgram.testingAction) {
}
}
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
static extern IntPtr SendMessageNew(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
const int WM_COMMAND = 0x111;
const int MIN_ALL = 419;
const int MIN_ALL_UNDO = 416;
public void MinimizeAll() {
if (MainProgram.testingAction) {
successMessage = "Simulated minimize of all windows";
} else {
IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);
SendMessageNew(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero);
successMessage = "Minimized all windows";
}
}
public void MaximizeAll() {
if (MainProgram.testingAction) {
successMessage = "Simulated minimize of all windows";
} else {
IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);
SendMessageNew(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero);
successMessage = "Minimized all windows";
}
}
//Maximize & minimize specific windows
private const int SW_MAXIMIZE = 3;
private const int SW_MINIMIZE = 6;
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public void MinimizeMaximizeWindow(string parameter, int type) {
bool paramIsNum = int.TryParse(parameter, out int pid);
if (paramIsNum) {
try {
//Results in an exception if process doesn't exist
Process theP = Process.GetProcessById(pid);
if (!MainProgram.testingAction) {
try {
ShowWindow(theP.MainWindowHandle, type);
successMessage = (type == SW_MAXIMIZE ? "Maximized" : "Minimized") + " process with ID " + pid.ToString();
} catch (Exception e) {
Error("Failed to " + (type == SW_MAXIMIZE ? "maximize" : "minimize") + " process with ID " + pid.ToString() + "; " + e.Message);
}
} else {
successMessage = "Successfully simulated " + (type == SW_MAXIMIZE ? "maximize" : "minimize") + " window(s)";
}
} catch {
Error("A process with the ID " + pid.ToString() + " doesn't exist");
}
} else {
try {
//Results in an exception if process doesn't exist
Process[] thePs = Process.GetProcessesByName(parameter);
if (!MainProgram.testingAction) {
try {
IntPtr hwnd = FindWindowByCaption(IntPtr.Zero, parameter);
ShowWindow(hwnd, type);
foreach (Process p in thePs) {
ShowWindow(p.MainWindowHandle, type);
}
successMessage = (type == SW_MAXIMIZE ? "Maximized" : "Minimized") + " all processes with name " + parameter;
} catch (Exception e) {
Error("Failed to " + (type == SW_MAXIMIZE ? "maximize" : "minimize") + " processes with name " + parameter + "; " + e.Message);
}
} else {
successMessage = "Successfully simulated " + (type == SW_MAXIMIZE ? "maximize" : "minimize") + " window(s)";
}
} catch {
Error("A process with the name " + parameter + " doesn't exist");
}
}
}
public void MinimizeWindow(string parameter) {
MinimizeMaximizeWindow(parameter, SW_MINIMIZE);
}
public void MaximizeWindow(string parameter) {
MinimizeMaximizeWindow(parameter, SW_MAXIMIZE);
}
public void WindowsToast() {
//Todo - quite a bit of work - later (pushed for way too many updates now, whoopppss)