forked from STY1001/Unowhy-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUT.cs
1900 lines (1710 loc) · 65.9 KB
/
UT.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
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
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Resources;
using System.Runtime.InteropServices;
using Unowhy_Tools_WPF.ViewModels;
using System.ComponentModel;
using Unowhy_Tools_WPF.Views;
using System.Windows.Forms;
using Unowhy_Tools_WPF.Views.Pages;
using System.Security.Principal;
using System.Collections.Generic;
using System.ServiceProcess;
using System.Runtime.CompilerServices;
using Unowhy_Tools_WPF.Views.Windows;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Net;
using System.Net.Http;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Windows.Interop;
using System.Threading.Tasks;
using System.Reflection;
using System.IO.Packaging;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media;
using System.Windows.Navigation;
using Wpf.Ui.Controls;
using System.IO.Pipes;
using System.IO.Compression;
using System.Threading;
using System.Drawing;
using TaskScheduler = Microsoft.Win32.TaskScheduler;
namespace Unowhy_Tools
{
public partial class UT
{
#region DLL
[DllImport("DwmApi")]
private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
[DllImport("wininet.dll")]
private static extern bool InternetGetConnectedState(out int state, int value);
#endregion
public static int verfull = 2400;
public static string verbuild = "1247030523";
public static bool verisdeb = true;
public class version
{
public static int getverfull()
{
return verfull;
}
public static bool isdeb()
{
return verisdeb;
}
public static string getverbuild()
{
return verbuild;
}
public static async Task<bool> newver()
{
var web = new HttpClient();
string newver = await web.GetStringAsync("https://bit.ly/UTnvTXT");
int newverint = Convert.ToInt32(newver);
if (verfull < newverint)
{
return true;
}
else
{
return false;
}
}
}
public class anim
{
public static Grid _grid;
public static Wpf.Ui.Controls.Button _button;
public static void TransitionForw(Grid grid)
{
var mainWindow = System.Windows.Application.Current.MainWindow as Unowhy_Tools_WPF.Views.MainWindow;
mainWindow.NavAnimRight();
_grid = grid;
DoubleAnimation anim = new DoubleAnimation();
anim.From = 0;
anim.To = -100;
anim.Duration = TimeSpan.FromMilliseconds(300);
anim.EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseInOut, Power = 5 };
TranslateTransform trans = new TranslateTransform();
_grid.RenderTransform = trans;
anim.Completed += setnormalForw;
trans.BeginAnimation(TranslateTransform.XProperty, anim);
}
public static void TransitionBack(Grid grid)
{
var mainWindow = System.Windows.Application.Current.MainWindow as Unowhy_Tools_WPF.Views.MainWindow;
mainWindow.NavAnimLeft();
_grid = grid;
DoubleAnimation anim = new DoubleAnimation();
anim.From = 0;
anim.To = 100;
anim.Duration = TimeSpan.FromMilliseconds(300);
anim.EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseInOut, Power = 5 };
TranslateTransform trans = new TranslateTransform();
_grid.RenderTransform = trans;
anim.Completed += setnormalBack;
trans.BeginAnimation(TranslateTransform.XProperty, anim);
}
public static void BackBtnAnim(Wpf.Ui.Controls.Button btn)
{
_button = btn;
DoubleAnimation anim = new DoubleAnimation();
anim.From = 0;
anim.To = -150;
anim.Duration = TimeSpan.FromMilliseconds(500);
anim.EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseInOut, Power = 5 };
TranslateTransform trans = new TranslateTransform();
_button.RenderTransform = trans;
anim.Completed += setnormalBackBTN;
trans.BeginAnimation(TranslateTransform.XProperty, anim);
}
public static void BackBtnAnimForw(Wpf.Ui.Controls.Button btn)
{
_button = btn;
DoubleAnimation anim = new DoubleAnimation();
anim.From = -150;
anim.To = 0;
anim.Duration = TimeSpan.FromMilliseconds(500);
anim.EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseInOut, Power = 5 };
TranslateTransform trans = new TranslateTransform();
_button.RenderTransform = trans;
anim.Completed += setnormalBackBTN;
trans.BeginAnimation(TranslateTransform.XProperty, anim);
}
public static void setnormalBackBTN(object sender, EventArgs e)
{
DoubleAnimation anim = new DoubleAnimation();
anim.From = -150;
anim.To = 0;
anim.Duration = TimeSpan.FromMilliseconds(0);
anim.EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseInOut, Power = 5 };
TranslateTransform trans = new TranslateTransform();
_button.RenderTransform = trans;
trans.BeginAnimation(TranslateTransform.XProperty, anim);
}
public static void setnormalBack(object sender, EventArgs e)
{
DoubleAnimation anim = new DoubleAnimation();
anim.From = -100;
anim.To = 0;
anim.Duration = TimeSpan.FromMilliseconds(300);
anim.EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseInOut, Power = 5 };
TranslateTransform trans = new TranslateTransform();
_grid.RenderTransform = trans;
anim.Completed += setnormalAnim;
trans.BeginAnimation(TranslateTransform.XProperty, anim);
}
public static void setnormalForw(object sender, EventArgs e)
{
DoubleAnimation anim = new DoubleAnimation();
anim.From = 100;
anim.To = 0;
anim.Duration = TimeSpan.FromMilliseconds(300);
anim.EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseInOut, Power = 5 };
TranslateTransform trans = new TranslateTransform();
_grid.RenderTransform = trans;
anim.Completed += setnormalAnim;
trans.BeginAnimation(TranslateTransform.XProperty, anim);
}
public static void setnormalAnim(object sender, EventArgs e)
{
var mainWindow = System.Windows.Application.Current.MainWindow as Unowhy_Tools_WPF.Views.MainWindow;
mainWindow.NavAnimNormal();
}
}
public class serv
{
public static async Task<bool> exist(string service)
{
Write2Log("Check " + service);
string s = await RunReturn("sc", "query \"" + service + "\"");
if (s.Contains("1060"))
{
return false;
}
else
{
return true;
}
}
public static async Task stop(string service)
{
Write2Log("Stop " + service);
await RunMin("net", "stop \"" + service + "\" /y");
}
public static async Task start(string service)
{
Write2Log("Start " + service);
await RunMin("net", "start \"" + service + "\"");
}
public static async Task auto(string service)
{
Write2Log("Enable " + service);
await RunMin("sc", "config \"" + service + "\" start=auto");
}
public static async Task dis(string service)
{
Write2Log("Disable " + service);
await serv.stop(service);
await RunMin("sc", "config \"" + service + "\" start=disabled");
}
public static async Task del(string service)
{
Write2Log("Delete " + service);
await serv.stop(service);
await RunMin("sc", "delete \"" + service + "\"");
}
}
public class waitstatus
{
public async static Task close()
{
var mainWindow = System.Windows.Application.Current.MainWindow as Unowhy_Tools_WPF.Views.MainWindow;
await mainWindow.HideWait();
Write2Log("Close wait");
}
public async static Task open()
{
var mainWindow = System.Windows.Application.Current.MainWindow as Unowhy_Tools_WPF.Views.MainWindow;
await mainWindow.ShowWait();
Write2Log("Open wait");
}
}
public class UTS
{
public static async Task<string> UTSmsg(string pipe, string msg)
{
string ret = null;
try
{
using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", pipe, PipeDirection.InOut, PipeOptions.Asynchronous))
{
await pipeClient.ConnectAsync();
if (pipeClient.IsConnected)
{
using (var writer = new StreamWriter(pipeClient))
using (var reader = new StreamReader(pipeClient))
{
await writer.WriteLineAsync(msg);
await writer.FlushAsync();
if (pipeClient.IsConnected)
{
ret = await reader.ReadLineAsync();
}
}
}
}
}
catch (Exception e)
{
}
return ret;
}
public static async Task UTScheck()
{
var mainWindow = System.Windows.Application.Current.MainWindow as Unowhy_Tools_WPF.Views.MainWindow;
mainWindow.SplashText.Text = "Preparing UTS... (Checking)";
string instdir = Directory.GetCurrentDirectory() + "\\Unowhy Tools Service";
if (!Directory.Exists(instdir))
{
Directory.CreateDirectory(instdir);
}
mainWindow.SplashBar.Value++;
mainWindow.SplashBar.Value++;
if (!File.Exists(instdir + "\\Unowhy Tools Service.exe"))
{
if (CheckInternet())
{
mainWindow.SplashText.Text = "Preparing UTS... (Downloading)";
var web = new HttpClient();
var filebyte = await web.GetByteArrayAsync("https://bit.ly/UTSzip");
string utemp = Path.GetTempPath() + "Unowhy Tools\\Temps";
File.WriteAllBytes(utemp + "\\service.zip", filebyte);
mainWindow.SplashText.Text = "Preparing UTS... (Extracting)";
await Task.Delay(100);
ZipFile.ExtractToDirectory(utemp + "\\service.zip", instdir);
await Task.Delay(100);
}
}
mainWindow.SplashBar.Value++;
mainWindow.SplashBar.Value++;
string utspath = instdir + "\\Unowhy Tools Service.exe";
if (await UT.serv.exist("UTS"))
{
ServiceController sc = new ServiceController();
sc.ServiceName = "UTS";
if (sc.ServiceType.HasFlag(ServiceType.InteractiveProcess) && sc.ServiceType.HasFlag(ServiceType.Win32OwnProcess))
{
}
else
{
await UT.RunMin("sc", "config UTS type=own type=interact");
mainWindow.SplashText.Text = "Preparing UTS... (Restarting)";
await UT.serv.stop("UTS");
await UT.serv.start("UTS");
}
if (sc.StartType == ServiceStartMode.Automatic)
{
if (sc.Status == ServiceControllerStatus.Running)
{
mainWindow.SplashText.Text = "Preparing UTS... (Running)";
}
else
{
await UT.serv.start("UTS");
mainWindow.SplashText.Text = "Preparing UTS... (Starting)";
}
}
else
{
await UT.serv.auto("UTS");
await UT.serv.start("UTS");
mainWindow.SplashText.Text = "Preparing UTS... (Starting)";
}
}
else
{
mainWindow.SplashText.Text = "Preparing UTS... (Installing)";
await UT.RunMin("sc", $"create UTS binpath=\"\\\"{utspath}\\\"\" displayname=\"Unowhy Tools Service\" start=auto type=own type=interact");
await UT.serv.start("UTS");
}
mainWindow.SplashBar.Value++;
mainWindow.SplashBar.Value++;
mainWindow.SplashText.Text = "Preparing UTS... (Checking Update)";
await Task.Delay(100);
await UT.UTS.UTSupdate();
mainWindow.SplashBar.Value++;
mainWindow.SplashBar.Value++;
}
public static async Task UTSupdate()
{
var mainWindow = System.Windows.Application.Current.MainWindow as Unowhy_Tools_WPF.Views.MainWindow;
if (CheckInternet())
{
var web = new HttpClient();
string newver = await web.GetStringAsync("https://bit.ly/UTStext");
newver = newver.Replace("\n", "").Replace("\r", "").Replace(" ", "");
if (!verisdeb)
{
string ver = await UT.UTS.UTSmsg("UTS", "GetVer");
if (!(newver == ver))
{
mainWindow.SplashText.Text = "Preparing UTS... (" + ver + " => " + newver + ")";
await Task.Delay(300);
mainWindow.SplashText.Text = "Preparing UTS... (Shutting down)";
await UT.serv.stop("UTS");
mainWindow.SplashText.Text = "Preparing UTS... (Downloading)";
string instdir = Directory.GetCurrentDirectory() + "\\Unowhy Tools Service";
Directory.Delete(instdir, true);
await Task.Delay(100);
Directory.CreateDirectory(instdir);
web = new HttpClient();
var filebyte = await web.GetByteArrayAsync("https://bit.ly/UTSzip");
string utemp = Path.GetTempPath() + "Unowhy Tools\\Temps";
File.WriteAllBytes(utemp + "\\service.zip", filebyte);
mainWindow.SplashText.Text = "Preparing UTS... (Extracting)";
await Task.Delay(100);
ZipFile.ExtractToDirectory(utemp + "\\service.zip", instdir);
await Task.Delay(100);
mainWindow.SplashText.Text = "Preparing UTS... (Starting up)";
await UT.serv.start("UTS");
}
}
}
}
}
public static async void NavigateTo(Type page)
{
var mainWindow = System.Windows.Application.Current.MainWindow as Unowhy_Tools_WPF.Views.MainWindow;
mainWindow.Navigate(page);
}
public static async Task Cleanup()
{
var mainWindow = System.Windows.Application.Current.MainWindow as Unowhy_Tools_WPF.Views.MainWindow;
/*
List<string> oldfiles = new List<string>()
{
"temp\\adminusers.txt",
"temp\\azure.txt",
"temp\\ene.txt",
"temp\\entuser.txt",
"temp\\gitversion.txt",
"temp\\hsmst.txt",
"temp\\ifp.txt",
"temp\\mf.txt",
"temp\\model.txt",
"temp\\os.txt",
"temp\\pcname.txt",
"temp\\rs.txt",
"temp\\shell.txt",
"temp\\username.txt",
"azureleave.exe",
"bootim.exe",
"cuaboff.exe",
"cuabon.exe",
"delent.exe",
"delentf.exe",
"delhismserv.exe",
"deloem.exe",
"delridf.exe",
"disadmin.exe",
"dishis.exe",
"enadmin.exe",
"enhis.exe",
"fixti.exe",
"fullsoftinfo.txt",
"fullpcinfo.txt",
"getpcinfo.exe",
"getsoftinfo.exe",
"getuserinfo.exe",
"langen.exe",
"langfr.exe",
"old2new",
"rdti.exe",
"reboot.exe",
"rmdirhismgr.exe",
"shell.exe",
"starthis.exe",
"stophis.exe",
"Unowhy Tools Updater.exe",
"utconfdel.exe",
"utkeydel.exe",
"version.txt",
"winhelloent.exe",
"winre.exe",
"update.zip",
"delhisqool.exe",
"ene.txt",
"model.txt",
"os.txt",
"pcname.txt",
"tversion.txt",
"username.txt",
"clog.html",
"fr.resx",
"en.resx",
"UTwait.exe",
"UTsplash.exe",
"7z.dll",
"7zip.exe"
};
foreach(string file in oldfiles)
{
mainWindow.SplashText.Text = "Cleanup... (Checking)";
mainWindow.SplashBar.Value++;
await Task.Delay(1);
if (File.Exists(file))
{
mainWindow.SplashText.Text = "Cleanup... (" + file + ")";
File.Delete(file);
}
}
*/
mainWindow.SplashBar.Value = 10;
mainWindow.SplashText.Text = "Cleanup... (Checking)";
if (Directory.Exists("temp"))
{
mainWindow.SplashText.Text = "Cleanup... (\\temp)";
Directory.Delete("temp", true);
}
await Task.Delay(300);
mainWindow.SplashBar.Value = 20;
mainWindow.SplashText.Text = "Cleanup... (Checking)";
if (Directory.Exists(Path.GetTempPath() + "\\Unowhy Tools\\Temps"))
{
mainWindow.SplashText.Text = "Cleanup... (%temp%\\Unowhy Tools\\Temps)";
Directory.Delete(Path.GetTempPath() + "\\Unowhy Tools\\Temps", true);
}
await Task.Delay(300);
mainWindow.SplashBar.Value = 30;
await Task.Delay(300);
}
public static async Task TrayCheck()
{
var mainWindow = System.Windows.Application.Current.MainWindow as Unowhy_Tools_WPF.Views.MainWindow;
mainWindow.SplashText.Text = "Preparing Tray... (Checking)";
await Task.Delay(300);
if (await CheckTray())
{
}
else
{
TaskScheduler.TaskService taskService = new TaskScheduler.TaskService();
TaskScheduler.Task uttask = taskService.GetTask("Unowhy Tools Tray Launch");
if (uttask == null)
{
mainWindow.SplashText.Text = "Preparing Tray... (Creating)";
TaskScheduler.TaskDefinition taskDefinition = taskService.NewTask();
taskDefinition.RegistrationInfo.Date = DateTime.Now;
taskDefinition.RegistrationInfo.Author = "Unowhy Tools";
taskDefinition.RegistrationInfo.Description = "Launch Unowhy Tools Tray at user logon. If your account isn't set as admin, tray startup can fail. Go to Unowhy Tools and set your account as admin.";
taskDefinition.RegistrationInfo.URI = @"\STY1001\Unowhy Tools\Unowhy Tools Tray Launch";
taskDefinition.Principal.GroupId = new SecurityIdentifier("S-1-5-32-544").ToString();
taskDefinition.Principal.RunLevel = TaskScheduler.TaskRunLevel.Highest;
taskDefinition.Settings.DisallowStartIfOnBatteries = false;
taskDefinition.Settings.StopIfGoingOnBatteries = false;
taskDefinition.Settings.AllowHardTerminate = true;
taskDefinition.Settings.StartWhenAvailable = true;
taskDefinition.Settings.RunOnlyIfNetworkAvailable = false;
taskDefinition.Settings.IdleSettings.StopOnIdleEnd = true;
taskDefinition.Settings.IdleSettings.RestartOnIdle = false;
taskDefinition.Settings.Enabled = true;
taskDefinition.Settings.Hidden = false;
taskDefinition.Settings.RunOnlyIfIdle = false;
taskDefinition.Settings.DisallowStartOnRemoteAppSession = false;
taskDefinition.Settings.UseUnifiedSchedulingEngine = true;
taskDefinition.Settings.WakeToRun = false;
taskDefinition.Settings.ExecutionTimeLimit = TimeSpan.Zero;
taskDefinition.Settings.Priority = System.Diagnostics.ProcessPriorityClass.Normal;
taskDefinition.Triggers.Add(new TaskScheduler.LogonTrigger { Enabled = true });
taskDefinition.Actions.Add(new TaskScheduler.ExecAction(Process.GetCurrentProcess().MainModule.FileName, "-tray", Directory.GetCurrentDirectory()));
taskService.RootFolder.RegisterTaskDefinition(@"Unowhy Tools Tray Launch", taskDefinition);
try
{
await Task.Delay(1000);
mainWindow.SplashText.Text = "Preparing Tray... (Launching)";
taskService = new TaskScheduler.TaskService();
uttask = taskService.GetTask("Unowhy Tools Tray Launch");
uttask.Run();
}
catch
{
mainWindow.SplashText.Text = "Preparing Tray... (Waiting)";
await Task.Delay(5000);
mainWindow.SplashText.Text = "Preparing Tray... (Launching)";
taskService = new TaskScheduler.TaskService();
uttask = taskService.GetTask("Unowhy Tools Tray Launch");
uttask.Run();
}
}
else
{
if (uttask.Enabled == true)
{
mainWindow.SplashText.Text = "Preparing Tray... (Launching)";
uttask.Run();
}
}
}
}
public static async Task<bool> FirstStart()
{
var mainWindow = System.Windows.Application.Current.MainWindow as Unowhy_Tools_WPF.Views.MainWindow;
mainWindow.SplashText.Text = "Checking... (Folder)";
await Task.Delay(100);
if (!Directory.Exists("C:\\UTSConfig"))
{
Directory.CreateDirectory("C:\\UTSConfig");
}
if (!File.Exists("C:\\UTSConfig\\serial.txt"))
{
File.WriteAllText("C:\\UTSConfig\\serial.txt", "Null");
}
mainWindow.SplashBar.Value++;
if (!Directory.Exists(Path.GetTempPath() + "\\Unowhy Tools"))
{
Directory.CreateDirectory(Path.GetTempPath() + "\\Unowhy Tools");
}
if (!Directory.Exists(Path.GetTempPath() + "\\Unowhy Tools\\Logs"))
{
Directory.CreateDirectory(Path.GetTempPath() + "\\Unowhy Tools\\Logs");
}
if (!Directory.Exists(Path.GetTempPath() + "\\Unowhy Tools\\Temps"))
{
Directory.CreateDirectory(Path.GetTempPath() + "\\Unowhy Tools\\Temps");
}
if (!Directory.Exists(Path.GetTempPath() + "\\Unowhy Tools\\Temps\\Update"))
{
Directory.CreateDirectory(Path.GetTempPath() + "\\Unowhy Tools\\Temps\\Update");
}
if (!Directory.Exists(Path.GetTempPath() + "\\Unowhy Tools\\Temps\\Drivers"))
{
Directory.CreateDirectory(Path.GetTempPath() + "\\Unowhy Tools\\Temps\\Drivers");
}
if (!Directory.Exists(Path.GetTempPath() + "\\Unowhy Tools\\Temps\\WebView2"))
{
Directory.CreateDirectory(Path.GetTempPath() + "\\Unowhy Tools\\Temps\\WebView2");
}
if (!Directory.Exists(Path.GetTempPath() + "\\Unowhy Tools\\Temps\\Service"))
{
Directory.CreateDirectory(Path.GetTempPath() + "\\Unowhy Tools\\Temps\\Service");
}
if (!File.Exists(Path.GetTempPath() + "\\Unowhy Tools\\Logs\\UT_Logs.txt"))
{
var f = File.CreateText(Path.GetTempPath() + "\\Unowhy Tools\\Logs\\UT_Logs.txt");
f.Close();
Write2Log("=== Unowhy Tools Logs ===");
}
mainWindow.SplashBar.Value++;
mainWindow.SplashText.Text = "Checking... (Registry)";
await Task.Delay(100);
RegistryKey keysoft = Registry.CurrentUser.OpenSubKey(@"Software", true);
RegistryKey keysty = Registry.CurrentUser.OpenSubKey(@"Software\STY1001", true);
if (keysty == null)
{
keysoft.CreateSubKey("STY1001");
}
keysty = Registry.CurrentUser.OpenSubKey(@"Software\STY1001", true);
RegistryKey keyut = Registry.CurrentUser.OpenSubKey(@"Software\STY1001\Unowhy Tools", true);
if (keyut == null)
{
keysty.CreateSubKey("Unowhy Tools");
}
mainWindow.SplashBar.Value++;
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\STY1001\Unowhy Tools", true);
object us = key.GetValue("UpdateStart", null);
if (us == null)
{
//await RunMin("reg", "add \"HKCU\\Software\\STY1001\\Unowhy Tools\" /v UpdateStart /d 1 /t REG_SZ /f");
key.SetValue("UpdateStart", "1", RegistryValueKind.String);
}
object o = key.GetValue("Lang", null);
if (o == null)
{
//await RunMin("reg", "add \"HKCU\\Software\\STY1001\\Unowhy Tools\" /v Lang /d EN /t REG_SZ /f");
key.SetValue("Lang", "EN", RegistryValueKind.String);
}
object i = key.GetValue("Init2", null);
if (i == null)
{
key.SetValue("Init2", "0", RegistryValueKind.String);
}
mainWindow.SplashBar.Value++;
string i2 = key.GetValue("Init2").ToString();
if (i2 == "1")
{
return false;
}
else
{
return true;
}
}
public static async Task<bool> CheckTray()
{
bool trayrun = false;
string tl = await RunReturn("powershell", "start-process -FilePath \"tasklist\" -ArgumentList \"/v\" -nonewwindow");
if (tl.Contains("Unowhy Tools Tray"))
{
trayrun = true;
Write2Log("UT Tray is already running");
}
else
{
Write2Log("UT Tray is not running");
}
return trayrun;
}
public static async Task<string> FolderSizeString(string directoryPath)
{
try
{
long size = 0;
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
await Task.Run(() =>
{
FileInfo[] files = directoryInfo.GetFiles("*", SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
Interlocked.Add(ref size, file.Length);
}
});
string rep = "0.00 B";
rep = FormatSize(size);
return rep;
}
catch
{
return "-.-- B";
}
}
public static string FormatSize(long byteSize)
{
const int scale = 1024;
string[] orders = { "GB", "MB", "KB", "B" };
long max = (long)Math.Pow(scale, orders.Length - 1);
foreach (string order in orders)
{
if (byteSize > max)
return string.Format("{0:0.00} {1}", (double)byteSize / max, order);
max /= scale;
}
return "0.00 B";
}
public static void Delay(int Time_delay)
{
int i = 0;
System.Timers.Timer _delayTimer = new System.Timers.Timer();
_delayTimer.Interval = Time_delay;
_delayTimer.AutoReset = false;
_delayTimer.Elapsed += (s, args) => i = 1;
_delayTimer.Start();
while (i == 0) { };
}
public static async Task<string> RunReturn(string file, string args)
{
IntPtr wow64Value = IntPtr.Zero;
Wow64DisableWow64FsRedirection(ref wow64Value);
Write2Log("RunReturn " + file + " " + args);
string output = await Task.Run(() =>
{
Process get = new Process();
get.StartInfo.FileName = file;
get.StartInfo.Arguments = args;
get.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
get.StartInfo.UseShellExecute = false;
get.StartInfo.RedirectStandardOutput = true;
get.StartInfo.CreateNoWindow = true;
get.Start();
get.WaitForExit();
return get.StandardOutput.ReadToEnd();
});
Write2Log("Done RunReturn " + file + " " + args + " => " + output);
return output;
}
public static string GetLine(string text, int line)
{
int line2 = line - 1;
var lines = text.Split('\n');
return lines[line2].Replace("\n", "").Replace("\r", "");
}
public static string GetLang(string name)
{
string resxFile = @".\lang\en.resx";
string enresx = @".\lang\en.resx";
string frresx = @".\lang\fr.resx";
RegistryKey ut = Registry.CurrentUser.OpenSubKey(@"Software\STY1001\Unowhy Tools", false);
if (ut != null)
{
object utl = ut.GetValue("Lang", null);
if (utl != null)
{
if (ut.GetValue("Lang").ToString() == "EN") resxFile = enresx;
else if (ut.GetValue("Lang").ToString() == "FR") resxFile = frresx;
ResXResourceSet resxSet1 = new ResXResourceSet(resxFile);
Write2Log("Get lang " + name + " => " + resxSet1.GetString(name));
return resxSet1.GetString(name);
}
ResXResourceSet resxSet2 = new ResXResourceSet(resxFile);
Write2Log("Get lang " + name + " => " + resxSet2.GetString(name));
return resxSet2.GetString(name);
}
ResXResourceSet resxSet3 = new ResXResourceSet(resxFile);
Write2Log("Get lang " + name + " => " + resxSet3.GetString(name));
return resxSet3.GetString(name);
/*
//Check the current saved language
string resxFile = @".\lang\en.resx";
RegistryKey utl = Registry.CurrentUser.OpenSubKey(@"Software\STY1001\Unowhy Tools", false);
string utls = utl.GetValue("Lang").ToString();
string enresx = @".\lang\en.resx";
string frresx = @".\lang\fr.resx";
//Chose the ResX file
if (utls == "EN") resxFile = enresx; //English
else if (utls == "FR") resxFile = frresx; //French
ResXResourceSet resxSet = new ResXResourceSet(resxFile);
Write2Log("Get lang " + name + " => " + resxSet.GetString(name));
return resxSet.GetString(name);
*/
}
public async static Task RunMin(string file, string args)
{
IntPtr wow64Value = IntPtr.Zero;
Wow64DisableWow64FsRedirection(ref wow64Value);
Write2Log("RunMin " + file + " " + args);
await Task.Run(() =>
{
Process p = new Process();
p.StartInfo.FileName = file;
p.StartInfo.Arguments = args;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.WaitForExit();
});
Write2Log("Done RunMin " + file + " " + args);
}
public static void Write2Log(string log)
{
if (File.Exists(Path.GetTempPath() + "\\Unowhy Tools\\Logs\\UT_Logs.txt"))
{
File.AppendAllText(Path.GetTempPath() + "\\Unowhy Tools\\Logs\\UT_Logs.txt", DateTime.Now.ToString() + " : " + log + Environment.NewLine);
}
}
public static void applylang_global()
{
}
public static bool CheckAdmin()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
public static void RunAdmin(string args)
{
// Restart and run as admin
var exeName = Process.GetCurrentProcess().MainModule.FileName;
ProcessStartInfo startInfo = new ProcessStartInfo(exeName);
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Directory.GetCurrentDirectory();
startInfo.Verb = "runas";
startInfo.Arguments = $"{args}";
Process.Start(startInfo);
System.Windows.Application.Current.Shutdown();
}
public static bool CheckInternet()
{
int Out;
if (InternetGetConnectedState(out Out, 0) == true) return true;
else return false;
}
public static async Task Check()
{
var mainWindow = System.Windows.Application.Current.MainWindow as Unowhy_Tools_WPF.Views.MainWindow;
Data UTdata = new Data();
mainWindow.SplashText.Text = "Checking... (Getting Hardware Info)";
await Task.Delay(100);
Write2Log("Getting PC Infos");
string hn = await RunReturn("hostname", "");
string mf = await RunReturn("wmic", "computersystem get manufacturer");
string md = await RunReturn("wmic", "computersystem get model");
string os = await RunReturn("wmic", "os get caption");
string bios = await RunReturn("wmic", "bios get smbiosbiosversion");
string sn = await RunReturn("wmic", "bios get serialnumber");
string cpu = await RunReturn("wmic", "cpu get name");
string ram = await RunReturn("wmic", "computersystem get totalphysicalmemory");
mainWindow.SplashBar.Value++;
UTdata.HostName = hn.Replace("\n", "").Replace("\r", "").Replace(" ", "");
UTdata.mf = GetLine(mf, 2);
UTdata.md = GetLine(md, 2);
UTdata.os = GetLine(os, 2);
UTdata.bios = GetLine(bios, 2);
UTdata.sn = GetLine(sn, 2);
UTdata.cpu = GetLine(cpu, 2);
UTdata.ram = GetLine(ram, 2);
if (UTdata.UserID.Contains(UTdata.HostName.ToLower()))
{
UTdata.User = UTdata.UserID.Replace(UTdata.HostName.ToLower() + "\\", "");
}
else if (UTdata.UserID.Contains("azuread"))
{
UTdata.User = UTdata.UserID;
UTdata.AADUser = true;
}
mainWindow.SplashBar.Value++;
Write2Log(UTdata.HostName);
Write2Log(UTdata.User);
Write2Log(UTdata.UserID);
Write2Log(UTdata.mf);
Write2Log(UTdata.md);
Write2Log(UTdata.os);
Write2Log(UTdata.bios);
Write2Log(UTdata.sn);
Write2Log(UTdata.cpu);
Write2Log(UTdata.ram);
Write2Log("Done");
mainWindow.SplashBar.Value++;
mainWindow.SplashText.Text = "Checking... (Getting Software Info)";
await Task.Delay(100);