-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallerOperations.cs
More file actions
80 lines (67 loc) · 2.51 KB
/
Copy pathInstallerOperations.cs
File metadata and controls
80 lines (67 loc) · 2.51 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
using System.IO;
namespace DataGateWin.Installer;
internal static class InstallerOperations
{
public static string ResolveUpdateInstallDir(
string startDir,
string exeName,
Action<string>? log = null,
int maxDepth = 6)
{
var current = startDir;
var checkedDirs = new List<string>();
for (var depth = 0; depth < maxDepth && !string.IsNullOrWhiteSpace(current); depth++)
{
checkedDirs.Add(current);
var candidateExe = Path.Combine(current, exeName);
if (File.Exists(candidateExe))
{
log?.Invoke($"Update mode: resolved install folder: {current}");
return current;
}
var parent = Directory.GetParent(current)?.FullName;
if (string.IsNullOrWhiteSpace(parent) ||
string.Equals(parent, current, StringComparison.OrdinalIgnoreCase))
{
break;
}
current = parent;
}
var attempts = string.Join(Environment.NewLine, checkedDirs.Select(d => $" - {d}"));
throw new FileNotFoundException(
$"DataGateWin.exe was not found near the installer. Checked:{Environment.NewLine}{attempts}",
Path.Combine(startDir, exeName));
}
public static void CopyDirectoryWithProgress(
string sourceDir,
string destinationDir,
Func<string, bool>? skipDestination,
IProgress<double> progress,
CancellationToken cancellationToken)
{
Directory.CreateDirectory(destinationDir);
var files = Directory.GetFiles(sourceDir, "*", SearchOption.AllDirectories);
var copyList = new List<(string Source, string Destination)>();
foreach (var file in files)
{
var relative = Path.GetRelativePath(sourceDir, file);
var destFile = Path.Combine(destinationDir, relative);
if (skipDestination?.Invoke(destFile) == true)
continue;
copyList.Add((file, destFile));
}
if (copyList.Count == 0)
{
progress.Report(100);
return;
}
for (var i = 0; i < copyList.Count; i++)
{
cancellationToken.ThrowIfCancellationRequested();
var (source, dest) = copyList[i];
Directory.CreateDirectory(Path.GetDirectoryName(dest)!);
File.Copy(source, dest, overwrite: true);
progress.Report((i + 1) * 100.0 / copyList.Count);
}
}
}