-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMainWindow.xaml.cs
674 lines (590 loc) · 28.1 KB
/
MainWindow.xaml.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
// Copyright (c) 2015, Dijji, and released under Ms-PL. This can be found in the root of this distribution.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Packaging;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using Microsoft.Win32;
namespace RepairTasks
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// It is important that the solution platform is Any CPU, as,
/// to work correctly, this program needs to run
/// in 64-bit on 64-bit machines, and 32-bit on 32-bit machines.
/// </summary>
public partial class MainWindow : Window
{
private State state = new State();
public MainWindow()
{
InitializeComponent();
this.DataContext = state;
}
private void scan_Click(object sender, RoutedEventArgs e)
{
state.Reset();
state.CanScan = false;
state.CanRepair = false;
state.Status = "Scanning...";
ThreadPool.QueueUserWorkItem(Scan, state);
}
private void repair_Click(object sender, RoutedEventArgs e)
{
if (rbRecycle.IsChecked == true)
state.Source = Source.Recycle;
else // other
state.Source = Source.Other;
// See if they want a backup
if (!Properties.Settings.Default.HasBackedUp)
{
MessageBoxResult r = MessageBox.Show("Repair will make changes to your system. If you have not made a backup of your task files, " +
"using the Backup Tasks button below, now might be a good time. Do you want to proceed with the Repair?",
"Proceed?", MessageBoxButton.YesNo);
if (r == MessageBoxResult.No)
return;
}
// We are using our downloaded zip file of Windows 7 task files, or an independent source of task XML files,
// so prompt for the zip file or folder that contains them
if (state.Source == Source.Other)
{
var dialog = new Ionic.Utils.FolderBrowserDialogEx
{
Description = "Select the downloaded zip file of Windows 7 task files, or a directory containing backed up task files.",
ShowNewFolderButton = false,
ShowEditBox = true,
NewStyle = true,
RootFolder = Environment.SpecialFolder.Desktop,
SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
ShowBothFilesAndFolders = true,
ShowFullPathInEditBox = false,
DontExpandZip = true,
ValidExtensions = new string[1] { "zip" },
};
if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
return;
else
{
if (Path.GetExtension(dialog.SelectedPath).ToLower() == ".zip")
{
if (!VerifyZip(state, dialog.SelectedPath))
return;
state.Source = Source.Zip;
}
state.SourcePath = dialog.SelectedPath;
}
}
state.Reports.Clear();
state.CanScan = false;
state.CanRepair = false;
state.Status = "Repairing...";
ThreadPool.QueueUserWorkItem(Repair, state);
}
private void unplug_Click(object sender, RoutedEventArgs e)
{
var target = (Target)((Report)lbReports.SelectedItem).Tag;
var result = MessageBox.Show(String.Format("This will unplug '{0}' from Task Scheduler. It will still be reported and available " +
"for repair by RepairTasks. Do you want to proceed?", target.FullName), "Unplug Task", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
state.Reports.Clear();
state.CanScan = true;
state.CanRepair = false;
Dictionary<string, string> dictRegKeys = new Dictionary<string, string>();
// Find the registry entries
FindRegistryEntries(target, dictRegKeys);
// Delete registry keys
foreach (string key in dictRegKeys.Keys)
Registry.LocalMachine.DeleteSubKey(key);
state.Reports.Add(new Report(String.Format("Task '{0}' is now unplugged from the Task Scheduler", target.FullName)));
state.Status = "Unplug completed";
}
}
private void backup_Click(object sender, RoutedEventArgs e)
{
state.Reports.Clear();
state.Status = "";
System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog
{
ShowNewFolderButton = false,
RootFolder = Environment.SpecialFolder.Desktop,
SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
Description = "Select a folder to create task backups in."
};
if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
return;
BackupTasks(dialog.SelectedPath);
}
private void save_Click(object sender, RoutedEventArgs e)
{
// Save the results of the last Scan or Repair
System.Windows.Forms.SaveFileDialog dialog = new System.Windows.Forms.SaveFileDialog();
dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
dialog.FilterIndex = 0;
dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
StreamWriter sw = new StreamWriter (dialog.OpenFile());
foreach (var r in state.Reports)
{
sw.WriteLine(r.Text);
}
sw.WriteLine(state.Status);
sw.Close();
}
}
private void lbReports_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var report = lbReports.SelectedItem as Report;
if (report != null)
{
var target = report.Tag as Target;
if (target != null)
{
state.CanUnplug = true;
return;
}
}
state.CanUnplug = false;
}
private static string rootDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Tasks") + @"\";
private static string logDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"LogFiles\Scm");
private static string hiddenSuffix = "._hidden_";
private static void Scan(Object obj)
{
State state = (State)obj;
int dirErrorCount = 0;
bool abnormal = false;
try
{
if (!Directory.Exists(logDir))
{
dirErrorCount++;
AddReport(state, String.Format("Required log file directory '{0}' is missing", logDir));
}
DirectoryInfo root = new DirectoryInfo(rootDir);
ScanDirectory(state, root);
}
catch (System.Exception ex)
{
AddReport(state, String.Format("Scan terminated by unexpected error '{0}'", ex.Message));
abnormal = true;
}
Application.Current.Dispatcher.Invoke(new Action(delegate
{
state.Status = String.Format("Scan completed{0}: {1} problems found", abnormal ? " abnormally" : "", dirErrorCount + state.Targets.Count);
state.CanScan = true;
state.CanRepair = (dirErrorCount + state.Targets.Count) > 0;
if (IsGangOfFive(state))
MessageBox.Show("These five tasks are typically left in an unusable state by reversion from Windows 10. To fix them, " +
"download Windows7 Tasks.zip from my site, if you have not done so already, " +
"then check the 'Take tasks from backup' radio button, click Repair, " +
"and select the downloaded zip file in the dialog that pops up", "Typical Windows 10 reversion errors");
}));
}
private static void ScanDirectory(State state, DirectoryInfo di)
{
string relPath = di.FullName.Replace(rootDir, "");
if (relPath.Length > 0)
relPath += @"\";
// Our backstop mechanism to avoid losing track of task files is to rename them with a special suffix,
// rather than deleting them, when we want to try reinstallation.
// Then, at the next scan, we clean the hidden files up, or restore them
List<FileInfo> hidden = new List<FileInfo>(di.GetFiles().Where(fi => fi.FullName.EndsWith(hiddenSuffix)));
foreach (var fi in hidden)
{
string realName = fi.FullName.Substring(0, fi.FullName.Length - hiddenSuffix.Length);
if (File.Exists(realName))
fi.Delete();
else
fi.CopyTo(realName);
}
foreach (var fi in di.GetFiles())
{
if (fi.FullName.EndsWith(hiddenSuffix))
{
continue;
}
try
{
Application.Current.Dispatcher.Invoke(new Action(delegate
{
state.Status = String.Format("Checking {0}...", relPath + fi.Name);
}));
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\schtasks.exe";
p.StartInfo.Arguments = "/query /tn \"" + relPath + fi.Name + "\"";
p.Start();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd().Replace("\r", "").Replace("\n", "");
p.WaitForExit();
if (p.ExitCode != 0)
{
var target = new Target { RelativePath = relPath, Name = fi.Name };
state.Targets.Add(target);
Application.Current.Dispatcher.Invoke(new Action(delegate
{
// TODO: break the dependence on the specific English form of these messages
if (error.StartsWith("ERROR: The task image is corrupt or has been tampered with."))
state.Reports.Add(new Report("Task image corrupt: " + relPath + fi.Name, target));
else if (error.StartsWith("ERROR: The system cannot find the file specified"))
state.Reports.Add(new Report("Task not installed: " + relPath + fi.Name));
else
state.Reports.Add(new Report(String.Format("Task {0} reported '{1}'", relPath + fi.Name, error), target));
}));
}
}
catch (System.Exception ex)
{
state.Targets.Add(new Target { RelativePath = relPath, Name = fi.Name });
AddReport(state, String.Format("Scan of task {0} terminated by unexpected error '{1}'", relPath + fi.Name, ex.Message));
}
}
foreach (var cdi in di.GetDirectories())
ScanDirectory(state, cdi);
}
private static string TaskCache = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\TaskCache\";
private static string[] Groups = { @"Plain\", @"Boot\", @"Logon\" };
private static string Tasks = @"Tasks\";
private static void Repair(Object obj)
{
State state = (State)obj;
int successCount = 0;
int failCount = 0;
bool abnormal = false;
try
{
if (!Directory.Exists(logDir))
{
try
{
Directory.CreateDirectory(logDir);
AddReport(state, String.Format("Created required log file directory '{0}'", logDir));
successCount++;
}
catch (System.Exception e)
{
AddReport(state, String.Format("Failed to create required log file directory '{0}', '{1}'", logDir, e.Message));
failCount++;
}
}
foreach (Target target in state.Targets)
{
string tempFilePath = Path.GetTempPath() + @"\" + target.Name;
Dictionary<string, string> dictRegKeys = new Dictionary<string, string>();
bool success = false;
bool renamedTask = false;
bool deletedKeys = false;
try
{
// Identify the task file to be installed
string taskFilePath = null;
if (state.Source == Source.Recycle)
{
try
{
// Copy the existing file to the temp folder
File.Copy(rootDir + target.FullName, tempFilePath);
}
catch (System.Exception e)
{
AddReport(state, String.Format("Cannot copy task file '{0}', {1}", rootDir + target.FullName, e.Message));
continue;
}
taskFilePath = tempFilePath;
}
else if (state.Source == Source.Zip)
{
// Try to unzip the required file into the temp folder
if (!UnzipTask(state, target.FullName, tempFilePath, state.SourcePath))
continue;
taskFilePath = tempFilePath;
}
else // if (state.Source == Source.Other)
{
taskFilePath = LocateTaskFileUnder(target, state.SourcePath);
if (taskFilePath == null)
{
AddReport(state, String.Format("Cannot find '{0}' under '{1}'", target.FullName, state.SourcePath));
continue;
}
else
{
AddReport(state, String.Format("Using '{0}' to repair '{1}'", taskFilePath, target.FullName));
}
}
// Find the registry entries
FindRegistryEntries(target, dictRegKeys);
// Save the registry entries
uint result = RegKey.ExportRegKeys(dictRegKeys);
if (result != 0)
{
AddReport(state, String.Format("Error {0} backing up registry keys for task {1}", result, target.FullName));
continue;
}
// Rename or delete existing task files
try
{
string hiddenTaskFile = rootDir + target.FullName + hiddenSuffix;
if (File.Exists(hiddenTaskFile))
File.Delete(hiddenTaskFile);
File.Move(rootDir + target.FullName, hiddenTaskFile);
// zap \windows\tasks\foo.job as well - if present, blocks create
string jobFile = Path.GetDirectoryName(Environment.SystemDirectory) + @"\tasks\" + target.Name + ".job";
if (File.Exists(jobFile))
File.Delete(jobFile);
renamedTask = true;
}
catch (System.Exception e)
{
AddReport(state, String.Format("Cannot delete task file '{0}', '{1}'", rootDir + target.FullName, e.Message));
continue;
}
// Delete registry keys
foreach (string key in dictRegKeys.Keys)
Registry.LocalMachine.DeleteSubKey(key);
deletedKeys = true;
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\schtasks.exe";
p.StartInfo.Arguments = "/create /tn \"" + target.FullName + "\" /xml \"" + taskFilePath + "\"";
p.Start();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd().Replace("\r", "").Replace("\n", "");
p.WaitForExit();
if (p.ExitCode == 0)
{
AddReport(state, "Recovered task: " + target.FullName);
successCount++;
success = true;
}
else
AddReport(state, String.Format("Recovery of task {0} failed with '{1}'", target.FullName, error), target);
}
catch (System.Exception ex)
{
AddReport(state, String.Format("Repair of task {0} terminated by unexpected error '{1}'", target.FullName, ex.Message));
}
finally
{
// If setup failed, restore the state so that the next scan picks it up
if (!success)
{
failCount++;
if (renamedTask)
RestoreTaskFile(state, tempFilePath, rootDir + target.FullName);
if (deletedKeys)
{
uint result = RegKey.RestoreRegKeys(dictRegKeys);
if (result != 0)
AddReport(state, String.Format("Error {0} restoring registry keys for task {1}", result, target.FullName));
}
}
// Clean up temp files
File.Delete(tempFilePath);
foreach (string tempFile in dictRegKeys.Values)
{
if (tempFile != null)
File.Delete(tempFile);
}
}
}
}
catch (System.Exception ex)
{
AddReport(state, String.Format("Repair terminated by unexpected error '{0}'", ex.Message));
abnormal = true;
}
Application.Current.Dispatcher.Invoke(new Action(delegate
{
state.Status = String.Format("Repair completed{0}: {1} repairs succeeded; {2} repairs failed",
abnormal ? " abnormally" : "", successCount, failCount);
state.CanScan = true;
}));
}
private static void AddReport(State state, string report, object tag = null)
{
Application.Current.Dispatcher.Invoke(new Action(delegate
{
state.Reports.Add(new Report(report, tag));
}));
}
private static bool VerifyZip(State state, string zipFile)
{
bool success = false;
try
{
using (Package package = Package.Open(zipFile, FileMode.Open, FileAccess.Read))
{
if (package.GetParts().Count() == 0)
MessageBox.Show(String.Format("'{0}' does not contain files in the correct format. " +
"Please check that you have downloaded the latest version of Windows7 Tasks.zip", zipFile), "Error");
else
success = true;
}
}
catch (System.Exception e)
{
AddReport(state, String.Format("Cannot open zip file '{0}: {1}", zipFile, e.Message));
}
return success;
}
private static bool UnzipTask(State state, string fullName, string tempFilePath, string zipFile)
{
bool success = false;
string zipUri = "/" + fullName.Replace(" ", "_").Replace(@"\", "/");
var partUri = new Uri(zipUri, UriKind.Relative);
try
{
using (Package package = Package.Open(zipFile, FileMode.Open, FileAccess.Read))
{
if (package.PartExists(partUri))
{
var part = package.GetPart(partUri);
using (Stream source = part.GetStream(FileMode.Open, FileAccess.Read))
using (Stream destination = File.OpenWrite(tempFilePath))
{
byte[] buffer = new byte[0x1000];
int read;
while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
{
destination.Write(buffer, 0, read);
}
success = true;
}
}
else
AddReport(state, String.Format("Zip file '{0}' does not contain task file '{1}'", zipFile, fullName));
}
}
catch (System.Exception e)
{
AddReport(state, String.Format("Cannot extract task file '{0}' from zip file '{1}': {2}", fullName, zipFile, e.Message));
}
return success;
}
private static string LocateTaskFileUnder(Target target, string path)
{
// Try for full path, and if not, look in root of folder
string fullPath = path + @"\" + target.FullName;
string shortPath = path + @"\" + target.Name;
if (File.Exists(fullPath))
return fullPath;
else if (File.Exists(shortPath))
return shortPath;
// No luck, walk the tree
DirectoryInfo di = new DirectoryInfo(path);
return LocateTaskFileHelper(target, di);
}
private static string LocateTaskFileHelper(Target target, DirectoryInfo di)
{
var file = di.GetFiles().Where(fi => fi.Name == target.Name).FirstOrDefault();
if (file != null)
{
// demand that immediately containing folders match
string dir = target.RelativePath.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries).Last();
if (dir == di.Name)
return file.FullName;
}
foreach (var dic in di.GetDirectories())
{
string f = LocateTaskFileHelper(target, dic);
if (f != null)
return f;
}
return null;
}
private static void FindRegistryEntries(Target target, Dictionary<string, string> dictRegKeys)
{
// Find the registry entries
string treeKeyPath = TaskCache + @"Tree\" + target.FullName;
string keyPath = null;
var treeKey = Registry.LocalMachine.OpenSubKey(treeKeyPath);
if (treeKey != null)
{
dictRegKeys.Add(treeKeyPath, null);
string id = treeKey.GetValue("Id") as string;
if (id != null)
{
// Check under Tasks
keyPath = TaskCache + Tasks + id;
if (null != Registry.LocalMachine.OpenSubKey(keyPath))
dictRegKeys.Add(keyPath, null);
// and the groups
foreach (string group in Groups)
{
keyPath = TaskCache + group + id;
if (null != Registry.LocalMachine.OpenSubKey(keyPath))
dictRegKeys.Add(keyPath, null);
}
}
}
}
private static void RestoreTaskFile(State state, string tempFile, string taskFile)
{
try
{
File.Copy(taskFile + hiddenSuffix, taskFile);
}
catch (System.Exception e)
{
AddReport(state, String.Format("Cannot restore task file '{0}', {1}", taskFile, e.Message));
}
}
private static bool IsGangOfFive(State state)
{
if (state.Targets.Count == 5)
{
var names = state.Targets.Select(t => t.FullName).ToList();
names.Sort();
return names[0] == @"Microsoft\Windows\PerfTrack\BackgroundConfigSurveyor" &&
names[1] == @"Microsoft\Windows\RAC\RacTask" &&
names[2] == @"Microsoft\Windows\Shell\WindowsParentalControls" &&
names[3] == @"Microsoft\Windows\Tcpip\IpAddressConflict1" &&
names[4] == @"Microsoft\Windows\Tcpip\IpAddressConflict2";
}
return false;
}
private void BackupTasks(string path)
{
string source = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Tasks");
string target = Path.Combine(path, String.Format("Tasks {0:yyyy-MM-dd HHmmss}", DateTime.Now));
Process p = new Process();
p.StartInfo.UseShellExecute = true;
p.StartInfo.CreateNoWindow = false;
p.StartInfo.FileName = "xcopy";
p.StartInfo.Arguments = "/S /I /E \"" + source + "\" \"" + target + "\"";
p.Start();
p.WaitForExit();
if (p.ExitCode == 0)
{
AddReport(state, "Backed up tasks to: " + target);
Properties.Settings.Default.HasBackedUp = true;
Properties.Settings.Default.Save();
}
else
{
AddReport(state, String.Format("Task back up failed with error '{0}'", p.ExitCode));
}
}
}
}