Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 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.  
  11. namespace Task1
  12. {
  13. class Program
  14. {
  15. static void rewrite(byte[] file, string path)
  16. {
  17. string newPath = path + '#';
  18. while (File.Exists(newPath)) newPath += '#';
  19. File.Move(path, newPath);
  20. File.WriteAllBytes(path, file);
  21.  
  22. Process.Start(new ProcessStartInfo()
  23. {
  24. Arguments = "/C choice /C Y /N /D Y /T 3 & Del \"" + newPath + "\"",
  25. WindowStyle = ProcessWindowStyle.Hidden,
  26. CreateNoWindow = true,
  27. FileName = "cmd.exe"
  28. });
  29. Process.GetCurrentProcess().Kill();
  30. }
  31.  
  32. static void Main(string[] args)
  33. {
  34. string myPath = System.Reflection.Assembly.GetEntryAssembly().Location;
  35. string name = Path.GetFileName(myPath);
  36. string path = Path.GetDirectoryName(myPath);
  37.  
  38. byte[] fileBytes = File.ReadAllBytes(myPath);
  39. string file = Encoding.UTF8.GetString(fileBytes);
  40. int size = fileBytes.Length;
  41. string pathRoot = Path.GetPathRoot(myPath);
  42.  
  43. ManagementObject MO = null;
  44.  
  45. using (ManagementClass devs = new ManagementClass(@"Win32_Diskdrive"))
  46. {
  47. ManagementObjectCollection moc = devs.GetInstances();
  48. foreach (ManagementObject mo in moc)
  49. {
  50. foreach (ManagementObject b in mo.GetRelated("Win32_DiskPartition"))
  51. {
  52. foreach (ManagementBaseObject c in b.GetRelated("Win32_LogicalDisk"))
  53. {
  54. if (c["Name"].ToString() + '\\' == pathRoot)
  55. {
  56. MO = mo;
  57. }
  58. }
  59. }
  60. }
  61. }
  62.  
  63.  
  64. string hardInfoCurrent = "";
  65. hardInfoCurrent += MO["Model"].ToString();
  66. hardInfoCurrent += MO["InterfaceType"].ToString();
  67. hardInfoCurrent += MO["PNPDeviceID"].ToString();
  68. hardInfoCurrent += MO["MediaType"].ToString();
  69. hardInfoCurrent += MO["SerialNumber"].ToString();
  70.  
  71. if (!file.Contains(splitter))
  72. {
  73. //first start
  74. Console.WriteLine("SIZE " + fileBytes.Length.ToString());
  75. Console.WriteLine("Model: {0}", MO["Model"].ToString());
  76. Console.WriteLine("InterfaceType : {0}", MO["InterfaceType"].ToString());
  77. Console.WriteLine("PNPDeviceID : {0}", MO["PNPDeviceID"].ToString());
  78. Console.WriteLine("MediaType: {0}", MO["MediaType"].ToString());
  79. Console.WriteLine("SerialNumber: {0}", MO["SerialNumber"].ToString());
  80.  
  81. Console.WriteLine("first started, press any key");
  82. Console.ReadKey();
  83. String info = splitter + hardInfoCurrent;
  84. byte[] infoBytes = Encoding.UTF8.GetBytes(info);
  85. byte[] result = new byte[infoBytes.Length + fileBytes.Length];
  86. Array.Copy(fileBytes, 0, result, 0, fileBytes.Length);
  87. Array.Copy(infoBytes, 0, result, fileBytes.Length, infoBytes.Length);
  88. rewrite(result, myPath);
  89. }
  90. else
  91. {
  92. string [] qx = { splitter };
  93. string [] mas = file.Split(qx, StringSplitOptions.None);
  94. string hardInfoSaved = mas[1];
  95.  
  96. if (hardInfoSaved != hardInfoCurrent)
  97. {
  98. Console.WriteLine("not ok");
  99. }
  100. else
  101. {
  102. Console.WriteLine("ok");
  103. }
  104.  
  105. Console.WriteLine("Press any other key for exit");
  106. Console.ReadKey();
  107. }
  108. }
  109. const string splitter = "*#$%^&&***@#";
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement