Guest User

Untitled

a guest
Jul 22nd, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Text.RegularExpressions;
  11. using System.Net;
  12.  
  13. namespace MD5Checker
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         static string filePath;
  18.         string file = string.Empty, md5 = string.Empty;
  19.         bool readFile = false;
  20.         bool readMd5 = false;
  21.         bool justRunned;
  22.         string[] lines;
  23.  
  24.         Dictionary<string, string> files;
  25.         StreamReader streamReader;
  26.         Label[,] labels;
  27.  
  28.         public Form1()
  29.         {
  30.             InitializeComponent();
  31.             systemChooser.Items.Add("Windows XP Proffesional");
  32.             systemChooser.Items.Add("Windows XP Home");
  33.             systemChooser.Items.Add("Windows 7 Home x86");
  34.             systemChooser.Items.Add("Windows 7 Professional x86");
  35.             systemChooser.Items.Add("Windows 7 Ultimate x86");
  36.             systemChooser.Items.Add("Windows 7 Home x64");
  37.             systemChooser.Items.Add("Windows 7 Professional x64");
  38.             systemChooser.Items.Add("Windows 7 Ultimate x64");
  39.         }
  40.  
  41.         private void formLoad(object sender, EventArgs e)
  42.         {
  43.             string singleLine;
  44.             files = new Dictionary<string, string>();
  45.  
  46.             //gdy odpalimy program automatycznie wczytuje xp prof
  47.             if (string.IsNullOrEmpty(filePath))
  48.             {
  49.                 filePath = "Windows XP Proffesional.set";
  50.                 justRunned = true;
  51.             }
  52.  
  53.             #region Czytanie zawartosci pliku
  54.             try
  55.             {
  56.                 streamReader = new StreamReader("Content/" + filePath);
  57.             }
  58.             catch
  59.             {
  60.                 MessageBox.Show(filePath + " is missing!",
  61.                         "File error",
  62.                         MessageBoxButtons.OK,
  63.                         MessageBoxIcon.Error,
  64.                         MessageBoxDefaultButton.Button1);
  65.                 Application.Exit();
  66.                
  67.             }
  68.             while ((singleLine = streamReader.ReadLine()) != null)
  69.             {
  70.                 if(singleLine.Contains("[") && singleLine.Contains("]"))
  71.                 {
  72.                     readFile = true;
  73.                 }
  74.  
  75.                 //czytamy nazwe pliku
  76.                 if (readFile)
  77.                 {
  78.                     file = singleLine;
  79.                     readFile = false;
  80.                     readMd5 = true;
  81.                 }
  82.  
  83.                 //czytamy hash MD5
  84.                 else if (readMd5)
  85.                 {
  86.                     md5 = singleLine;
  87.                 }
  88.  
  89.                 //gdy oba zostały wyszukane dodaje je do słownika
  90.                 if (!string.IsNullOrEmpty(md5) && !string.IsNullOrEmpty(file))
  91.                 {
  92.                     file = file.Replace("[", "");
  93.                     file = file.Replace("]", "");
  94.                     file = file.Trim();
  95.                     md5 = md5.Trim();
  96.                     files.Add(file, md5);
  97.                     md5 = string.Empty;
  98.                     file = string.Empty;
  99.                     readFile = false;
  100.                     readMd5 = false;
  101.                 }
  102.             }
  103.  
  104.             #endregion
  105.  
  106.             //tworzy tablice labelow w zaleznosci od ilości plików w *.set
  107.             labels = new Label[files.Count, 2];
  108.  
  109.             int x = 0;
  110.             foreach (KeyValuePair<string, string> pair in files)
  111.             {
  112.                 Point point = new Point(10, 30);
  113.                 point.Y += 15 * (x + 1);
  114.  
  115.                 labels[x, 0] = new Label();
  116.                 labels[x, 0].Text = pair.Key + ":";
  117.                 labels[x, 0].Location = point;
  118.                 labels[x, 0].Size = new Size(80, 13);
  119.  
  120.                 point.X += 80;
  121.  
  122.                 labels[x, 1] = new Label();
  123.                 labels[x, 1].Text = pair.Value;
  124.                 labels[x, 1].Location = point;
  125.                 labels[x, 1].Size = new Size(220, 13);
  126.  
  127.                 this.Controls.Add(labels[x, 0]);
  128.                 this.Controls.Add(labels[x, 1]);
  129.  
  130.                 x++;
  131.             }
  132.  
  133.             //gdy odpalimy program to ustawia text z comboboxa na xp proffesional
  134.             if (justRunned)
  135.             {
  136.                 systemChooser.Text = filePath.Replace(".set", "");
  137.                 justRunned = false;
  138.             }
  139.         }
  140.  
  141.         private void checkButton_Click(object sender, EventArgs e)
  142.         {
  143.             Regex r = new Regex("\\n");
  144.             int i = 0;
  145.  
  146.             foreach (KeyValuePair<string, string> pair in files)
  147.             {
  148.                 labels[i, 1].ForeColor = Color.Black;
  149.                 labels[i, 1].BackColor = Color.FromArgb(240, 240, 240);
  150.                 i++;
  151.             }
  152.  
  153.             //manualne czytanie
  154.             if (manualButton.Checked)
  155.             {
  156.                 if (string.IsNullOrEmpty(inputBox.Text))
  157.                 {
  158.                     MessageBox.Show("Input is empty!",
  159.                         "ERROR",
  160.                         MessageBoxButtons.OK,
  161.                         MessageBoxIcon.Error,
  162.                         MessageBoxDefaultButton.Button1);
  163.                     return;
  164.                 }
  165.  
  166.                 lines = r.Split(inputBox.Text);
  167.             }
  168.  
  169.             //czytanie zawartosci z url
  170.             else if (internetButton.Checked)
  171.             {
  172.                 if (string.IsNullOrEmpty(internetBox.Text))
  173.                 {
  174.                     MessageBox.Show("Input is empty!!",
  175.                         "ERROR",
  176.                         MessageBoxButtons.OK,
  177.                         MessageBoxIcon.Error,
  178.                         MessageBoxDefaultButton.Button1);
  179.                     return;
  180.                 }
  181.  
  182.                
  183.                 try
  184.                 {
  185.                     downloadingLabel.Text = "Downloading...";
  186.                     string url = internetBox.Text;
  187.                     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  188.                     Application.DoEvents();
  189.  
  190.                     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  191.                     StreamReader sr = new StreamReader(response.GetResponseStream());
  192.  
  193.                     Application.DoEvents();
  194.  
  195.                     string tempString = sr.ReadToEnd();
  196.                     lines = r.Split(tempString);
  197.                     sr.Close();
  198.                    
  199.                 }
  200.                 catch
  201.                 {
  202.                     MessageBox.Show("Problem with internet connection or wrong URL!",
  203.                         "ERROR",
  204.                         MessageBoxButtons.OK,
  205.                         MessageBoxIcon.Exclamation,
  206.                         MessageBoxDefaultButton.Button1);
  207.                     downloadingLabel.Text = "";
  208.                     return;
  209.                 }
  210.             }
  211.  
  212.             downloadingLabel.Text = "";
  213.  
  214.             bool checkMd5 = false;
  215.  
  216.             i = 0;
  217.             foreach (KeyValuePair<string, string> pair in files)
  218.             {
  219.                 foreach (string l in lines)
  220.                 {
  221.                     if (l.Contains(pair.Key))
  222.                     {
  223.                         checkMd5 = true;
  224.                         continue;
  225.                     }
  226.  
  227.                     if (checkMd5)
  228.                     {
  229.                         if (l.Contains(pair.Value))
  230.                         {
  231.                             labels[i, 1].BackColor = Color.Green;
  232.                             labels[i, 1].ForeColor = Color.White;
  233.                         }
  234.                         else
  235.                         {
  236.                             labels[i, 1].BackColor = Color.Red;
  237.                             labels[i, 1].ForeColor = Color.White;
  238.                         }
  239.                         checkMd5 = false;
  240.                         break;
  241.                     }
  242.                 }
  243.                 i++;
  244.             }
  245.  
  246.             i=0;
  247.             foreach (KeyValuePair<string, string> pair in files)
  248.             {
  249.                 if (labels[i, 1].ForeColor != Color.White)
  250.                     labels[i, 1].ForeColor = Color.FromArgb(200, 200, 200);
  251.                 i++;
  252.             }
  253.            
  254.         }
  255.  
  256.         private void manualButton_CheckedChanged(object sender, EventArgs e)
  257.         {
  258.             if (manualButton.Checked)
  259.             {
  260.                 inputBox.Enabled = true;
  261.                 internetBox.Enabled = false;
  262.             }
  263.             else if (internetButton.Checked)
  264.             {
  265.                 inputBox.Enabled = false;
  266.                 internetBox.Enabled = true;
  267.             }
  268.         }
  269.  
  270.         private void systemChooser_SelectedIndexChanged(object sender, EventArgs e)
  271.         {
  272.            
  273.             filePath = systemChooser.Text + ".set";
  274.  
  275.             foreach (Label l in labels)
  276.             {
  277.                 this.Controls.Remove(l);
  278.             }
  279.  
  280.             formLoad(sender, e);
  281.         }
  282.  
  283.         private void inputBox_KeyDown(object sender, KeyEventArgs e)
  284.         {
  285.             if (e.Control & e.KeyCode == Keys.A)
  286.                 inputBox.SelectAll();
  287.         }
  288.  
  289.         private void internetBox_KeyDown(object sender, KeyEventArgs e)
  290.         {
  291.             if (e.Control & e.KeyCode == Keys.A)
  292.                 internetBox.SelectAll();
  293.         }
  294.     }
  295. }
Add Comment
Please, Sign In to add comment