Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 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 ConsoleAppication2
  14. {
  15. class Program
  16. {
  17. static void rewrite(byte[] file, string path)
  18. {
  19.  
  20. string newPath = Path.GetTempPath() + '#';
  21.  
  22. while (File.Exists(newPath))
  23. newPath += '#';
  24.  
  25.  
  26. while (File.Exists(newPath)) newPath += '#';
  27. File.Move(path, newPath);
  28. File.WriteAllBytes(path, file);
  29.  
  30. Process.Start(new ProcessStartInfo()
  31. {
  32. Arguments = "/C choice /C Y /N /D Y /T 3 & Del \"" + newPath + "\"",
  33. WindowStyle = ProcessWindowStyle.Hidden,
  34. CreateNoWindow = true,
  35. FileName = "cmd.exe"
  36. });
  37. Process.GetCurrentProcess().Kill();
  38. }
  39.  
  40. static void Main(string[] args)
  41. {
  42.  
  43. string myPath = System.Reflection.Assembly.GetEntryAssembly().Location;
  44. string name = Path.GetFileName(myPath);
  45. string path = Path.GetDirectoryName(myPath);
  46.  
  47. byte[] fileBytes = File.ReadAllBytes(myPath);
  48. string file = Encoding.UTF8.GetString(fileBytes);
  49. int size = fileBytes.Length;
  50. string pathRoot = Path.GetPathRoot(myPath);
  51.  
  52. ManagementObject MO = null;
  53.  
  54. using (ManagementClass devs = new ManagementClass(@"Win32_Diskdrive"))
  55. {
  56. ManagementObjectCollection moc = devs.GetInstances();
  57. foreach (ManagementObject mo in moc)
  58. {
  59. foreach (ManagementObject b in mo.GetRelated("Win32_DiskPartition"))
  60. {
  61. foreach (ManagementBaseObject c in b.GetRelated("Win32_LogicalDisk"))
  62. {
  63. if (c["Name"].ToString() + '\\' == pathRoot)
  64. {
  65. MO = mo;
  66. }
  67. }
  68. }
  69. }
  70. }
  71.  
  72.  
  73. string hardInfoCurrent = "";
  74. hardInfoCurrent += MO["Model"].ToString();
  75. hardInfoCurrent += MO["InterfaceType"].ToString();
  76. hardInfoCurrent += MO["PNPDeviceID"].ToString();
  77. hardInfoCurrent += MO["MediaType"].ToString();
  78. hardInfoCurrent += MO["SerialNumber"].ToString();
  79.  
  80.  
  81. FileIOPermission writePermission = new FileIOPermission(
  82. FileIOPermissionAccess.Write, path);
  83. if (!SecurityManager.IsGranted(writePermission)){
  84. Console.WriteLine("No access to write!");
  85. return;
  86. }
  87. if (!file.Contains(splitter))
  88. {
  89. //first start
  90. Console.WriteLine("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 anykey for exit");
  132. Console.ReadKey();
  133. }
  134. }
  135. const string splitter = "*#$%^&№#%#%#%#%#%#%#%#W^^^^^^&***@#";
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement