Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Management;
- using System.Diagnostics;
- using System.Threading;
- using System.Security;
- using System.Security.Permissions;
- namespace ConsoleAppication1
- {
- class Program
- {
- static void rewrite(byte[] file, string path)
- {
- FileIOPermission writePermission = new FileIOPermission(
- FileIOPermissionAccess.Write, path);
- if (!SecurityManager.IsGranted(writePermission))
- throw new Exception();
- string newPath = Path.GetTempPath() + '#';
- while (File.Exists(newPath))
- newPath += '#';
- while (File.Exists(newPath)) newPath += '#';
- File.Move(path, newPath);
- File.WriteAllBytes(path, file);
- Process.Start(new ProcessStartInfo()
- {
- Arguments = "/C choice /C Y /N /D Y /T 3 & Del \"" + newPath + "\"",
- WindowStyle = ProcessWindowStyle.Hidden,
- CreateNoWindow = true,
- FileName = "cmd.exe"
- });
- Process.GetCurrentProcess().Kill();
- }
- static void Main(string[] args)
- {
- string myPath = System.Reflection.Assembly.GetEntryAssembly().Location;
- string name = Path.GetFileName(myPath);
- string path = Path.GetDirectoryName(myPath);
- byte[] fileBytes = File.ReadAllBytes(myPath);
- string file = Encoding.UTF8.GetString(fileBytes);
- int size = fileBytes.Length;
- string pathRoot = Path.GetPathRoot(myPath);
- ManagementObject MO = null;
- using (ManagementClass devs = new ManagementClass(@"Win32_Diskdrive"))
- {
- ManagementObjectCollection moc = devs.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- foreach (ManagementObject b in mo.GetRelated("Win32_DiskPartition"))
- {
- foreach (ManagementBaseObject c in b.GetRelated("Win32_LogicalDisk"))
- {
- if (c["Name"].ToString() + '\\' == pathRoot)
- {
- MO = mo;
- }
- }
- }
- }
- }
- string hardInfoCurrent = "";
- hardInfoCurrent += MO["Model"].ToString();
- hardInfoCurrent += MO["InterfaceType"].ToString();
- hardInfoCurrent += MO["PNPDeviceID"].ToString();
- hardInfoCurrent += MO["MediaType"].ToString();
- hardInfoCurrent += MO["SerialNumber"].ToString();
- if (!file.Contains(splitter))
- {
- //first start
- Console.WriteLine(Environment.NewLine + "Temp Path = " + Path.GetTempPath() + Environment.NewLine);
- Console.WriteLine("Model: {0}", MO["Model"].ToString());
- Console.WriteLine("InterfaceType : {0}", MO["InterfaceType"].ToString());
- Console.WriteLine("PNPDeviceID : {0}", MO["PNPDeviceID"].ToString());
- Console.WriteLine("MediaType: {0}", MO["MediaType"].ToString());
- Console.WriteLine("SerialNumber: {0}", MO["SerialNumber"].ToString());
- Console.WriteLine("first started, press any key");
- Console.ReadKey();
- String info = splitter + hardInfoCurrent;
- byte[] infoBytes = Encoding.UTF8.GetBytes(info);
- byte[] result = new byte[infoBytes.Length + fileBytes.Length];
- Array.Copy(fileBytes, 0, result, 0, fileBytes.Length);
- Array.Copy(infoBytes, 0, result, fileBytes.Length, infoBytes.Length);
- try
- {
- rewrite(result, myPath);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
- else
- {
- string[] qx = { splitter };
- string[] mas = file.Split(qx, StringSplitOptions.None);
- string hardInfoSaved = mas[1];
- if (hardInfoSaved != hardInfoCurrent)
- {
- Console.WriteLine("not ok");
- }
- else
- {
- Console.WriteLine("ok");
- }
- Console.WriteLine("Press any other key for exit");
- Console.ReadKey();
- }
- }
- const string splitter = "*#$%^&&***@#";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement