Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.14 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using Microsoft.Win32;
  4. using System.IO;
  5. using System.Diagnostics;
  6.  
  7. namespace StartupScanner
  8. {
  9.     public partial class Form1 : Form
  10.     {
  11.         public Form1()
  12.         {
  13.             InitializeComponent();
  14.             rbackup.Checked = true;
  15.         }
  16.      
  17.         private void Export(string exportPath, string registryPath)
  18.         {
  19.             string path = "\"" + exportPath + "\"";
  20.             string key = "\"" + registryPath + "\"";
  21.             Process proc = new Process();
  22.  
  23.             try
  24.             {
  25.                 proc.StartInfo.FileName = "regedit.exe";
  26.                 proc.StartInfo.UseShellExecute = false;
  27.  
  28.                 proc = Process.Start("regedit.exe", "/e " + path + " " + key);
  29.                 proc.WaitForExit();
  30.             }
  31.             catch (Exception)
  32.             {
  33.                 proc.Dispose();
  34.             }
  35.         }
  36.  
  37.         private void Import(string registryPath)
  38.         {
  39.             if (!File.Exists(registryPath))
  40.             {
  41.                 MessageBox.Show(registryPath + " does not exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  42.                 return;
  43.             }
  44.  
  45.             string key = "\"" + registryPath + "\"";
  46.             Process proc = new Process();
  47.  
  48.             try
  49.             {
  50.                 proc.StartInfo.FileName = "regedit.exe";
  51.                 proc.StartInfo.UseShellExecute = false;
  52.  
  53.                 proc = Process.Start("regedit.exe", "/s " + key);
  54.                 proc.WaitForExit();
  55.                
  56.             }
  57.             catch (Exception)
  58.             {
  59.                 proc.Dispose();
  60.             }
  61.         }
  62.         private void scanbtn (object sender, EventArgs e)
  63.         {
  64.                 listView1.Items.Clear();
  65.                 RegistryKey key, key1, keyx86;
  66.                 String location = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";//HKCU,HKLM
  67.                 string location1 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce";//HKCU
  68.                 if (Environment.Is64BitOperatingSystem)
  69.                 {
  70.                     key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
  71.                     key1 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
  72.                     keyx86 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
  73.                 }
  74.                 else
  75.                 {
  76.                     key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);
  77.                     key1 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
  78.                     keyx86 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
  79.                 }
  80.  
  81.                 string[] valuenames = key.OpenSubKey(location).GetValueNames();
  82.                 string[] valuenames1 = key1.OpenSubKey(location).GetValueNames();
  83.                 string[] valuename2 = key.OpenSubKey(location1).GetValueNames();
  84.                 string[] valuenames3 = keyx86.OpenSubKey(location).GetValueNames();
  85.                 /////////////////// HKCU_Run//////////////////////////////////////////
  86.                 foreach (string sub in valuenames)
  87.                 {
  88.                     object data = key.OpenSubKey(location).GetValue(sub);
  89.                     ListViewItem lvi = new ListViewItem(sub);
  90.                     lvi.SubItems.Add(data.ToString());
  91.                     lvi.SubItems.Add("HKCU_Run");
  92.                     listView1.Items.Add(lvi);
  93.  
  94.                 }
  95.                 //////////////////////////// HKCU_RunOne///////////////////////////
  96.  
  97.                 foreach (string sub in valuename2)
  98.                 {
  99.                     object data2 = key.OpenSubKey(location1).GetValue(sub);
  100.                     ListViewItem lvi = new ListViewItem(sub);
  101.                     lvi.SubItems.Add(data2.ToString());
  102.                     lvi.SubItems.Add("HKCU_RunOne");
  103.                     listView1.Items.Add(lvi);
  104.                 }
  105.                 ///////////////////// HKLM_Run/////////////////////////////////
  106.  
  107.  
  108.                 foreach (string sub in valuenames1)
  109.                 {
  110.                     object data1 = key1.OpenSubKey(location).GetValue(sub);
  111.                     ListViewItem lvi = new ListViewItem(sub);
  112.                     lvi.SubItems.Add(data1.ToString());
  113.                     lvi.SubItems.Add("HKLM_Runx64");
  114.                     listView1.Items.Add(lvi);
  115.                 }
  116.                 foreach (string sub in valuenames3)
  117.                 {
  118.                     object data3 = keyx86.OpenSubKey(location).GetValue(sub);
  119.                     ListViewItem lvi = new ListViewItem(sub);
  120.                     lvi.SubItems.Add(data3.ToString());
  121.                     lvi.SubItems.Add("HKLM_Runx86");
  122.                     listView1.Items.Add(lvi);
  123.                 }
  124.  
  125.                 listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
  126.                 listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
  127.                 count.Text = "Total: " + listView1.Items.Count.ToString() + " rows";
  128.             }
  129.  
  130.      
  131.  
  132.         private void Form1_Load(object sender, EventArgs e)
  133.         {
  134.             listView1.View = View.Details;
  135.             listView1.GridLines = true;
  136.             listView1.FullRowSelect = true;
  137.             listView1.MultiSelect = true;
  138.  
  139.         }
  140.  
  141.         private void removebtn (object sender, EventArgs e)
  142.         {
  143.  
  144.                 RegistryKey key,key1,keyx86;
  145.                 var selecteditems = listView1.SelectedItems;
  146.                 String location = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
  147.                 string location1 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce";
  148.                 if (Environment.Is64BitOperatingSystem)
  149.                 {
  150.                     key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64);
  151.                     key1= RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
  152.                     keyx86 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
  153.                 }
  154.                 else
  155.                 {
  156.                     key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32);
  157.                     key1 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
  158.                     keyx86 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
  159.                 }
  160.  
  161.              
  162.                 foreach (ListViewItem sub in selecteditems)
  163.                 {
  164.                 if (sub.SubItems[2].Text == "HKCU_Run")
  165.                 {
  166.                     key.OpenSubKey(location, true).DeleteValue(sub.SubItems[0].Text);
  167.                     key.Close();
  168.                     listView1.Items.Remove(sub);
  169.                 }
  170.                 else if(sub.SubItems[2].Text=="HKCU_RunOne")
  171.                 {
  172.                     key.OpenSubKey(location1, true).DeleteValue(sub.SubItems[0].Text);
  173.                     key.Close();
  174.                     listView1.Items.Remove(sub);
  175.                 }
  176.                 else if(sub.SubItems[2].Text=="HKLM_Runx64")
  177.                 {
  178.                     key1.OpenSubKey(location, true).DeleteValue(sub.SubItems[0].Text);
  179.                     key1.Close();
  180.                     listView1.Items.Remove(sub);
  181.                 }
  182.                 else
  183.                 {
  184.                     keyx86.OpenSubKey(location, true).DeleteValue(sub.SubItems[0].Text);
  185.                     keyx86.Close();
  186.                     listView1.Items.Remove(sub);
  187.                 }
  188.                 count.Text = "Total: " + listView1.Items.Count.ToString() + " rows";
  189.             }
  190.             }
  191.  
  192.         private void button3_Click(object sender, EventArgs e)
  193.         {
  194.            
  195.             try
  196.             {
  197.                 string tree = null;
  198.                 ListViewItem selecteditems = listView1.SelectedItems[0];
  199.                 string registryLocation = null;
  200.                 if (selecteditems.SubItems[2].Text.Substring(0, 4) == "HKLM")
  201.                 {
  202.  
  203.                     tree = "HKEY_LOCAL_MACHINE";
  204.                     registryLocation = Path.Combine(tree, @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  205.                 }
  206.                 else
  207.                 {
  208.                     tree = "HKEY_CURRENT_USER";
  209.                    
  210.                     if (selecteditems.SubItems[2].Text.Substring(5,3) == "Run")
  211.                     {
  212.                         registryLocation = Path.Combine(tree, @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  213.                     }
  214.                    else
  215.                     {
  216.                         registryLocation = Path.Combine(tree, @"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce");
  217.                        
  218.                     }
  219.  
  220.                 }
  221.                
  222.                 var registryLastKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit";
  223.                 Registry.SetValue(registryLastKey, "LastKey", registryLocation); // Set LastKey value that regedit will go directly to
  224.                 Process.Start("regedit.exe");
  225.             }
  226.             catch (Exception ex)
  227.             {
  228.                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  229.             }
  230.         }
  231.  
  232.         private void btngo_Click(object sender, EventArgs e)
  233.         {
  234.             String HKCU_Run = @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
  235.             string HKLM_Run= @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
  236.             string HKCU_RunOnce=@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce";
  237.             string HKLM_RunOnce = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce";
  238.             try
  239.             {
  240.                 string folderbk = AppDomain.CurrentDomain.BaseDirectory +"Regbackup";
  241.  
  242.                 if (!Directory.Exists(folderbk))
  243.                 {
  244.                     Directory.CreateDirectory(folderbk);
  245.                 }
  246.  
  247.  
  248.                 if (rbackup.Checked == true)
  249.                 {
  250.                     Export(folderbk + @"\HKCU_Run.reg", HKCU_Run);
  251.                     Export(folderbk + @"\HKLM_Run.reg", HKLM_Run);
  252.                     Export(folderbk + @"\HKCU_RunOnce.reg", HKCU_RunOnce);
  253.                     Export(folderbk + @"\HKLM_RunOnce.reg", HKLM_RunOnce);
  254.                     MessageBox.Show("Registry backups completed!");
  255.                 }
  256.                 else
  257.                 {
  258.                     Import(folderbk + @"\HKCU_Run.reg");
  259.                     Import(folderbk + @"\HKLM_Run.reg");
  260.                     Import(folderbk + @"\HKCU_RunOnce.reg");
  261.                     Import(folderbk + @"\HKLM_RunOnce.reg");
  262.                     MessageBox.Show("Registry restores completed!");
  263.                 }
  264.             }
  265.             catch(Exception ex)
  266.             {
  267.                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  268.             }
  269.  
  270.            
  271.         }
  272.     }
  273.  
  274.        
  275.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement