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
|
class AutoDeleter
{
public static bool WindowsIsClosing { get; protected set; }
static AutoDeleter()
{
WindowsIsClosing = false;
Microsoft.Win32.SystemEvents.SessionEnding += delegate
{
WindowsIsClosing = true;
};
Microsoft.Win32.SystemEvents.SessionEnded += delegate
{
WindowsIsClosing = true;
};
}
static string GetTmpBatName()
{
const string model = "{0}\\DS_Unist{1}.bat";
string WinTmpDir = Path.GetTempPath();
int nm = 0;
while (File.Exists(String.Format(model,WinTmpDir,nm.ToString())))
{
nm++;
}
return String.Format(model, WinTmpDir, nm.ToString());
}
[Flags]
public enum MoveFileFlags
{
MOVEFILE_REPLACE_EXISTING = 0x00000001,
MOVEFILE_COPY_ALLOWED = 0x00000002,
MOVEFILE_DELAY_UNTIL_REBOOT = 0x00000004,
MOVEFILE_WRITE_THROUGH = 0x00000008,
MOVEFILE_CREATE_HARDLINK = 0x00000010,
MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x00000020
}
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName,
MoveFileFlags dwFlags);
public static bool IsFullASCIIString(string str)
{
/*
* Windows pour l'instant (même jusqu'à Vista) ne supporte
* pas l'exécution (via cmd) de fichiers bat qui contient
* des caractères accentués.
*
* Dans le cas où on en a, on va donc repasser
* vers le MoveFileEx ...
* */
byte[] b = Encoding.ASCII.GetBytes(str);
string ascii_equivalent = Encoding.ASCII.GetString(b);
return str.Equals(ascii_equivalent);
}
public static void AutoDeleterStart()
{
string file_name = Assembly.GetEntryAssembly().Location;
if (!IsFullASCIIString(file_name) || WindowsIsClosing)
{
/* Si Windows est en train de se fermer, on va se supprimer au prochain redémarrage */
MoveFileEx(file_name, null, MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT);
}
else
{
Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath);
FileStream fi = null;
int try_count = 20;
do
{
try { fi = new FileStream(GetTmpBatName(), FileMode.Create); }
catch { }
try_count--;
System.Threading.Thread.Sleep(1000);
}
while (try_count >= 0 && fi == null);
if (try_count < 0)
{
MessageBox.Show("Impossible de supprimer certains fichiers temporraires car la création du fichier d'auto-suppression a échouée",
"Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (fi == null)
fi = new FileStream(GetTmpBatName(), FileMode.Create); /* erreur possible, si le do a échoué ... */
string name = fi.Name;
StreamWriter wr = new StreamWriter(fi, Encoding.Default);
wr.WriteLine("@echo off");
wr.WriteLine("REM DreamShield.IO.Utils AutoDeleter Bat");
wr.WriteLine("REM BatGenerator v 1.0");
wr.WriteLine();
wr.WriteLine(":del_process");
wr.WriteLine(String.Format("@if exist \"{0}\" del \"{0}\"", file_name));
wr.WriteLine(String.Format("@if exist \"{0}\" goto del_process", file_name));
wr.WriteLine();
wr.WriteLine(String.Format("del \"{0}\"", name));
wr.Flush();
fi.Flush();
fi.Close();
ProcessStartInfo info = new ProcessStartInfo();
info.WindowStyle = ProcessWindowStyle.Hidden;
info.FileName = name;
Process.Start(info);
}
}
} |
Partager