Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Management;
  8. using System.Diagnostics;
  9. using System.Threading;
  10. using System.Security;
  11. using System.Security.Permissions;
  12.  
  13. namespace ConsoleAppication1
  14. {
  15. class Program
  16. {
  17. static void rewrite(byte[] file, string path)
  18. {
  19.  
  20. FileIOPermission writePermission = new FileIOPermission(
  21. FileIOPermissionAccess.Write, path);
  22. if (!SecurityManager.IsGranted(writePermission))
  23. throw new Exception();
  24.  
  25.  
  26. string newPath = Path.GetTempPath() + '#';
  27.  
  28. while (File.Exists(newPath))
  29. newPath += '#';
  30.  
  31.  
  32. while (File.Exists(newPath)) newPath += '#';
  33. File.Move(path, newPath);
  34. File.WriteAllBytes(path, file);
  35.  
  36. Process.Start(new ProcessStartInfo()
  37. {
  38. Arguments = "/C choice /C Y /N /D Y /T 3 & Del \"" + newPath + "\"",
  39. WindowStyle = ProcessWindowStyle.Hidden,
  40. CreateNoWindow = true,
  41. FileName = "cmd.exe"
  42. });
  43. Process.GetCurrentProcess().Kill();
  44. }
  45.  
  46. static void Main(string[] args)
  47. {
  48.  
  49. string myPath = System.Reflection.Assembly.GetEntryAssembly().Location;
  50. string name = Path.GetFileName(myPath);
  51. string path = Path.GetDirectoryName(myPath);
  52.  
  53. byte[] fileBytes = File.ReadAllBytes(myPath);
  54. string file = Encoding.UTF8.GetString(fileBytes);
  55. int size = fileBytes.Length;
  56. string pathRoot = Path.GetPathRoot(myPath);
  57.  
  58. ManagementObject MO = null;
  59.  
  60. using (ManagementClass devs = new ManagementClass(@"Win32_Diskdrive"))
  61. {
  62. ManagementObjectCollection moc = devs.GetInstances();
  63. foreach (ManagementObject mo in moc)
  64. {
  65. foreach (ManagementObject b in mo.GetRelated("Win32_DiskPartition"))
  66. {
  67. foreach (ManagementBaseObject c in b.GetRelated("Win32_LogicalDisk"))
  68. {
  69. if (c["Name"].ToString() + '\\' == pathRoot)
  70. {
  71. MO = mo;
  72. }
  73. }
  74. }
  75. }
  76. }
  77.  
  78.  
  79. string hardInfoCurrent = "";
  80. hardInfoCurrent += MO["Model"].ToString();
  81. hardInfoCurrent += MO["InterfaceType"].ToString();
  82. hardInfoCurrent += MO["PNPDeviceID"].ToString();
  83. hardInfoCurrent += MO["MediaType"].ToString();
  84. hardInfoCurrent += MO["SerialNumber"].ToString();
  85.  
  86. if (!file.Contains(splitter))
  87. {
  88. //first start
  89.  
  90. Console.WriteLine(Environment.NewLine + "Temp Path = " + Path.GetTempPath() + Environment.NewLine);
  91.  
  92.  
  93. Console.WriteLine("Model: {0}", MO["Model"].ToString());
  94. Console.WriteLine("InterfaceType : {0}", MO["InterfaceType"].ToString());
  95. Console.WriteLine("PNPDeviceID : {0}", MO["PNPDeviceID"].ToString());
  96. Console.WriteLine("MediaType: {0}", MO["MediaType"].ToString());
  97. Console.WriteLine("SerialNumber: {0}", MO["SerialNumber"].ToString());
  98.  
  99. Console.WriteLine("first started, press any key");
  100. Console.ReadKey();
  101. String info = splitter + hardInfoCurrent;
  102. byte[] infoBytes = Encoding.UTF8.GetBytes(info);
  103. byte[] result = new byte[infoBytes.Length + fileBytes.Length];
  104. Array.Copy(fileBytes, 0, result, 0, fileBytes.Length);
  105. Array.Copy(infoBytes, 0, result, fileBytes.Length, infoBytes.Length);
  106.  
  107. try
  108. {
  109. rewrite(result, myPath);
  110. }
  111. catch (Exception e)
  112. {
  113. Console.WriteLine(e.Message);
  114. }
  115. }
  116. else
  117. {
  118. string[] qx = { splitter };
  119. string[] mas = file.Split(qx, StringSplitOptions.None);
  120. string hardInfoSaved = mas[1];
  121.  
  122. if (hardInfoSaved != hardInfoCurrent)
  123. {
  124. Console.WriteLine("not ok");
  125. }
  126. else
  127. {
  128. Console.WriteLine("ok");
  129. }
  130.  
  131. Console.WriteLine("Press any other key for exit");
  132. Console.ReadKey();
  133. }
  134. }
  135. const string splitter = "*#$%^&&***@#";
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement