Annotation of cleanflash/CleanFlashUninstaller/Program.cs, revision 1.1

1.1     ! root        1: using System;
        !             2: using System.Diagnostics;
        !             3: using System.Windows.Forms;
        !             4: using System.IO;
        !             5: using System.Runtime.InteropServices;
        !             6: 
        !             7: namespace CleanFlashUninstaller {
        !             8:     static class Program {
        !             9:         [Flags]
        !            10:         internal enum MoveFileFlags {
        !            11:             None = 0,
        !            12:             ReplaceExisting = 1,
        !            13:             CopyAllowed = 2,
        !            14:             DelayUntilReboot = 4,
        !            15:             WriteThrough = 8,
        !            16:             CreateHardlink = 16,
        !            17:             FailIfNotTrackable = 32,
        !            18:         }
        !            19: 
        !            20:         [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        !            21:         static extern bool MoveFileEx(
        !            22:             string lpExistingFileName,
        !            23:             string lpNewFileName,
        !            24:             MoveFileFlags dwFlags);
        !            25: 
        !            26:         static bool DeleteOnReboot(string filename) {
        !            27:             return MoveFileEx(filename, null, MoveFileFlags.DelayUntilReboot);
        !            28:         }
        !            29: 
        !            30:         static string TrimPath(string path) {
        !            31:             return path.TrimEnd(new[] { '/', '\\' }); 
        !            32:         }
        !            33: 
        !            34:         static bool EnsureRunInTemp() {
        !            35:             string tempFolder = TrimPath(Path.GetTempPath());
        !            36:             string currentPath = Application.ExecutablePath;
        !            37:             string currentFolder = TrimPath(Path.GetDirectoryName(currentPath));
        !            38:                 
        !            39:             if (currentFolder.Equals(tempFolder, StringComparison.OrdinalIgnoreCase)) {
        !            40:                 // Already running in the temp directory.
        !            41:                 return true;
        !            42:             }
        !            43: 
        !            44:             string currentExeName = Path.GetFileName(currentPath);
        !            45:             string newPath = Path.Combine(tempFolder, currentExeName);
        !            46: 
        !            47:             if (File.Exists(newPath)) {
        !            48:                 try {
        !            49:                     // Attempt to delete the old version of the uninstaller.
        !            50:                     File.Delete(newPath);
        !            51:                 } catch {
        !            52:                     // Uninstaller is already running.
        !            53:                     Application.Exit();
        !            54:                     return false;
        !            55:                 }
        !            56:             }
        !            57: 
        !            58:             // Copy the new file and mark it as delete-on-reboot
        !            59:             File.Copy(currentPath, newPath);
        !            60:             DeleteOnReboot(newPath);
        !            61: 
        !            62:             // Start the new process and end the old one
        !            63:             Process.Start(newPath);
        !            64:             Application.Exit();
        !            65:             return false;
        !            66:         }
        !            67: 
        !            68:         /// <summary>
        !            69:         /// The main entry point for the application.
        !            70:         /// </summary>
        !            71:         [STAThread]
        !            72:         static void Main() {
        !            73:             if (!EnsureRunInTemp()) {
        !            74:                 return;
        !            75:             }
        !            76: 
        !            77:             Application.EnableVisualStyles();
        !            78:             Application.SetCompatibleTextRenderingDefault(false);
        !            79:             Application.Run(new UninstallForm());
        !            80:         }
        !            81:     }
        !            82: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.