Advertisement
Guest User

Untitled

a guest
May 26th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. public static bool WindowsIsClosing { get; protected set; }
  2. class AutoDeleter
  3. {
  4.  
  5. static AutoDeleter()
  6. {
  7. WindowsIsClosing = false;
  8. Microsoft.Win32.SystemEvents.SessionEnding += delegate
  9. {
  10. WindowsIsClosing = true;
  11. };
  12. Microsoft.Win32.SystemEvents.SessionEnded += delegate
  13. {
  14. WindowsIsClosing = true;
  15. };
  16. }
  17.  
  18. static string GetTmpBatName()
  19. {
  20. const string model = "{0}\\DS_Unist{1}.bat";
  21.  
  22. string WinTmpDir = Path.GetTempPath();
  23. int nm = 0;
  24. while (File.Exists(String.Format(model, WinTmpDir, nm.ToString())))
  25. {
  26. nm++;
  27. }
  28.  
  29. return String.Format(model, WinTmpDir, nm.ToString());
  30. }
  31.  
  32. [Flags]
  33. public enum MoveFileFlags
  34. {
  35. MOVEFILE_REPLACE_EXISTING = 0x00000001,
  36. MOVEFILE_COPY_ALLOWED = 0x00000002,
  37. MOVEFILE_DELAY_UNTIL_REBOOT = 0x00000004,
  38. MOVEFILE_WRITE_THROUGH = 0x00000008,
  39. MOVEFILE_CREATE_HARDLINK = 0x00000010,
  40. MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x00000020
  41. }
  42.  
  43. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  44. public static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName,
  45. MoveFileFlags dwFlags);
  46.  
  47. public static void AutoDeleterStart()
  48. {
  49. string file_name = Assembly.GetEntryAssembly().Location;
  50.  
  51. if (WindowsIsClosing)
  52. {
  53.  
  54. MoveFileEx(file_name, null, MoveFileFlags.MOVEFILE_DELAY_UNTIL_REBOOT);
  55. }
  56. else
  57. {
  58. Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  59.  
  60. FileStream fi = null;
  61.  
  62. int try_count = 20;
  63. do
  64. {
  65. try { fi = new FileStream(GetTmpBatName(), FileMode.Create); }
  66. catch { }
  67. try_count--;
  68.  
  69. System.Threading.Thread.Sleep(1000);
  70. }
  71. while (try_count >= 0 && fi == null);
  72.  
  73. if (try_count < 0)
  74. {
  75. return;
  76. }
  77.  
  78. if (fi == null)
  79. fi = new FileStream(GetTmpBatName(), FileMode.Create); /* erreur possible, si le do a échoué ... */
  80.  
  81. string name = fi.Name;
  82.  
  83. StreamWriter wr = new StreamWriter(fi, Encoding.Default);
  84. wr.WriteLine("@echo off");
  85. wr.WriteLine("REM DreamShield.IO.Utils AutoDeleter Bat");
  86. wr.WriteLine("REM BatGenerator v 1.0");
  87. wr.WriteLine();
  88.  
  89. wr.WriteLine(":del_process");
  90. wr.WriteLine(String.Format("@if exist \"{0}\" del \"{0}\"", file_name));
  91. wr.WriteLine(String.Format("@if exist \"{0}\" goto del_process", file_name));
  92. wr.WriteLine();
  93.  
  94. wr.WriteLine(String.Format("del \"{0}\"", name));
  95.  
  96. wr.Flush();
  97. fi.Flush();
  98. fi.Close();
  99.  
  100. ProcessStartInfo info = new ProcessStartInfo();
  101. info.WindowStyle = ProcessWindowStyle.Hidden;
  102. info.FileName = name;
  103. Process.Start(info);
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement