-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppInstall.cs
More file actions
195 lines (167 loc) · 6.8 KB
/
Copy pathAppInstall.cs
File metadata and controls
195 lines (167 loc) · 6.8 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
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace MeetMicSync;
/// <summary>
/// Copies the app into a per-user Programs folder and manages the Startup shortcut.
/// No admin rights required.
/// </summary>
internal static class AppInstall
{
public const string AppFolderName = "MeetMicSync";
public const string ExeName = "MeetMicSync.exe";
public const string ShortcutName = "Meet Mic Sync.lnk";
public static string InstallDirectory =>
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Programs",
AppFolderName);
public static string InstalledExePath => Path.Combine(InstallDirectory, ExeName);
public static string StartupShortcutPath =>
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Startup),
ShortcutName);
public static string CurrentExePath =>
Environment.ProcessPath
?? Process.GetCurrentProcess().MainModule?.FileName
?? Path.Combine(AppContext.BaseDirectory, ExeName);
public static bool IsRunningFromInstallDir()
{
try
{
return PathsEqual(CurrentExePath, InstalledExePath);
}
catch
{
return false;
}
}
public static bool StartupShortcutExists() => File.Exists(StartupShortcutPath);
/// <summary>
/// Plain-language explanation shown before any changes are made.
/// </summary>
public static string BuildConfirmMessage()
{
var sb = new StringBuilder();
sb.AppendLine("Meet Mic Sync will do the following:");
sb.AppendLine();
if (!IsRunningFromInstallDir())
{
sb.AppendLine("1. Copy this program to your user Programs folder:");
sb.AppendLine($" {InstallDirectory}");
sb.AppendLine(" (no administrator rights needed; your Downloads copy can stay where it is)");
sb.AppendLine();
sb.AppendLine("2. Create a Startup shortcut so the copied program starts when you sign in to Windows:");
}
else
{
sb.AppendLine("1. Create a Startup shortcut so this program starts when you sign in to Windows:");
}
sb.AppendLine($" {StartupShortcutPath}");
sb.AppendLine();
if (!IsRunningFromInstallDir())
{
sb.AppendLine("3. Restart Meet Mic Sync from the new folder.");
sb.AppendLine();
}
sb.AppendLine("You can remove the Startup shortcut later from the tray menu.");
sb.AppendLine();
sb.Append("Continue?");
return sb.ToString();
}
public static string BuildRemoveConfirmMessage()
{
return
"Remove Meet Mic Sync from Windows Startup?" + Environment.NewLine +
Environment.NewLine +
"This will delete only the Startup shortcut:" + Environment.NewLine +
StartupShortcutPath + Environment.NewLine +
Environment.NewLine +
"The program files will stay on disk. Meet Mic Sync will not start automatically after you sign in.";
}
/// <summary>
/// Install/copy + create Startup shortcut. May request a restart from the install path.
/// </summary>
public static InstallResult EnableStartup()
{
try
{
Directory.CreateDirectory(InstallDirectory);
var source = CurrentExePath;
var needsRestart = !PathsEqual(source, InstalledExePath);
if (needsRestart)
{
// A running .exe can be copied; replacing the file we are running from is avoided.
File.Copy(source, InstalledExePath, overwrite: true);
Log.Write($"install: copied '{source}' → '{InstalledExePath}'");
}
else
{
Log.Write("install: already running from install directory");
}
CreateShortcut(StartupShortcutPath, InstalledExePath, InstallDirectory);
Log.Write($"install: startup shortcut → '{StartupShortcutPath}'");
return new InstallResult(
Success: true,
NeedsRestart: needsRestart,
Message: needsRestart
? "Installed and added to Startup. Restarting from the user Programs folder…"
: "Added to Windows Startup.");
}
catch (Exception ex)
{
Log.Write($"install failed: {ex}");
return new InstallResult(false, false, $"Could not set up Startup: {ex.Message}");
}
}
public static InstallResult DisableStartup()
{
try
{
if (File.Exists(StartupShortcutPath))
{
File.Delete(StartupShortcutPath);
Log.Write($"install: removed startup shortcut '{StartupShortcutPath}'");
}
return new InstallResult(true, false, "Removed from Windows Startup.");
}
catch (Exception ex)
{
Log.Write($"uninstall startup failed: {ex}");
return new InstallResult(false, false, $"Could not remove Startup shortcut: {ex.Message}");
}
}
public static void RestartFromInstalledCopy()
{
var psi = new ProcessStartInfo
{
FileName = InstalledExePath,
WorkingDirectory = InstallDirectory,
UseShellExecute = true
};
Process.Start(psi);
}
private static void CreateShortcut(string shortcutPath, string targetPath, string workingDirectory)
{
// IWshRuntimeLibrary via ProgID — available on all desktop Windows, no extra package.
var shellType = Type.GetTypeFromProgID("WScript.Shell")
?? throw new InvalidOperationException("WScript.Shell is not available on this PC.");
dynamic shell = Activator.CreateInstance(shellType)
?? throw new InvalidOperationException("Could not create WScript.Shell.");
dynamic shortcut = shell.CreateShortcut(shortcutPath);
shortcut.TargetPath = targetPath;
shortcut.WorkingDirectory = workingDirectory;
shortcut.WindowStyle = 1; // normal
shortcut.Description = "Meet Mic Sync — Lenovo mic mute ↔ Google Meet";
shortcut.Save();
Marshal.FinalReleaseComObject(shortcut);
Marshal.FinalReleaseComObject(shell);
}
private static bool PathsEqual(string a, string b) =>
string.Equals(
Path.GetFullPath(a).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar),
Path.GetFullPath(b).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar),
StringComparison.OrdinalIgnoreCase);
}
internal readonly record struct InstallResult(bool Success, bool NeedsRestart, string Message);