diff --git a/src/Agent/AutopilotMonitor.Agent/buildcounter.txt b/src/Agent/AutopilotMonitor.Agent/buildcounter.txt index 816d01be..1bb7ac53 100644 --- a/src/Agent/AutopilotMonitor.Agent/buildcounter.txt +++ b/src/Agent/AutopilotMonitor.Agent/buildcounter.txt @@ -1 +1 @@ -420 +421 diff --git a/src/Agent/AutopilotMonitor.SummaryDialog/App.xaml.cs b/src/Agent/AutopilotMonitor.SummaryDialog/App.xaml.cs index 35a14aef..b6259e7f 100644 --- a/src/Agent/AutopilotMonitor.SummaryDialog/App.xaml.cs +++ b/src/Agent/AutopilotMonitor.SummaryDialog/App.xaml.cs @@ -189,12 +189,19 @@ namespace AutopilotMonitor.SummaryDialog Log($"SelfCleanup: spawning PowerShell to delete '{exeDir}' after PID {pid} exits"); + // WorkingDirectory MUST NOT be exeDir — on Windows a process cannot + // delete a directory that is its own CWD. When the agent launches + // the dialog via CreateProcessAsUser it sets CWD = exeDir, and + // PowerShell would inherit that, making Remove-Item silently fail. + var safeDir = Environment.GetFolderPath(Environment.SpecialFolder.System); + Process.Start(new ProcessStartInfo { FileName = psExe, Arguments = $"-NoProfile -NonInteractive -WindowStyle Hidden -EncodedCommand {encoded}", UseShellExecute = true, - WindowStyle = ProcessWindowStyle.Hidden + WindowStyle = ProcessWindowStyle.Hidden, + WorkingDirectory = safeDir }); Log("SelfCleanup: cleanup process started");