I have by myself added extra function on .net side to perform files deletion afterwards. It generates powershell script, which launches and waits for installer to exit.
Code:
/// <summary> /// Installation uses .net framework assembly, but problem is that application itself cannot delete temporary folder, due to share violation with .net framework. /// We start here powershell on background, which will wait for main process to exist, and perform folder / file deletion after that one. /// </summary> [DllExport("CleanupAfterApplicationExit")] public static void CleanupAfterApplicationExit() { try { var procId = Process.GetCurrentProcess().Id; String dllPath = System.Reflection.Assembly.GetExecutingAssembly().Location; String tempDir = Path.GetDirectoryName(dllPath); // For testing purposes only, Setup factory normally places in temp folder named with "_ir_sf_temp" if (!tempDir.Contains("_ir_sf_temp")) { tempDir = Path.Combine(tempDir, "_ir_sf_temp"); if (!Directory.Exists(tempDir)) { Directory.CreateDirectory(tempDir); } File.Copy(dllPath, Path.Combine(tempDir, Path.GetFileName(dllPath)), true); } String ps1Path = Path.Combine(Path.GetDirectoryName(tempDir), $"0_cleanup_{procId}.ps1"); String ps1 = $"$proc = [System.Diagnostics.Process]::GetProcessById({procId})\n" + "$proc.WaitForExit()\n" + $"Remove-Item -Recurse -Force \"{tempDir}\"\n" + $"Remove-Item -Path \"{ps1Path}\"\n" ; File.WriteAllText(ps1Path, ps1); var process = new Process(); process.StartInfo.UseShellExecute = true; process.StartInfo.RedirectStandardOutput = false; process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized; process.StartInfo.FileName = @"C:\windows\system32\windowspowershell\v1.0\powershell.exe"; process.StartInfo.Arguments = "-executionpolicy bypass -file \"" + ps1Path + "\""; process.Start(); } catch (Exception ex) { String msg = $"{ex.Message}\r\ncallstack: {ex.StackTrace}"; EventLog.WriteEntry(".NET Runtime", msg, EventLogEntryType.Error, 1000); } }
Leave a comment: