GUI/CrashForm.cs
author moel.mich
Sun, 27 May 2012 14:23:31 +0000
changeset 344 3145aadca3d2
parent 206 1fa8eddc24a7
permissions -rw-r--r--
Changed the license to the Mozilla Public License 2.0 and update the licensing information.
moel@150
     1
/*
moel@344
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@344
     6
 
moel@344
     7
  Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@344
     9
*/
moel@150
    10
moel@150
    11
moel@150
    12
using System;
moel@150
    13
using System.IO;
moel@150
    14
using System.Net;
moel@150
    15
using System.Text;
moel@150
    16
using System.Windows.Forms;
moel@150
    17
moel@150
    18
namespace OpenHardwareMonitor.GUI {
moel@150
    19
  public partial class CrashForm : Form {
moel@150
    20
moel@150
    21
    private Exception exception;
moel@150
    22
moel@150
    23
    public CrashForm() {
moel@150
    24
      InitializeComponent();
moel@150
    25
    }
moel@150
    26
moel@150
    27
    public Exception Exception {
moel@150
    28
      get { return exception; }
moel@150
    29
      set {
moel@150
    30
        exception = value;
moel@150
    31
        StringBuilder s = new StringBuilder();
moel@150
    32
        Version version = typeof(CrashForm).Assembly.GetName().Version;
moel@150
    33
        s.Append("Version: "); s.AppendLine(version.ToString());        
moel@150
    34
        s.AppendLine();
moel@150
    35
        s.AppendLine(exception.ToString());
moel@150
    36
        s.AppendLine();
moel@150
    37
        if (exception.InnerException != null) {
moel@150
    38
          s.AppendLine(exception.InnerException.ToString());
moel@150
    39
          s.AppendLine();
moel@150
    40
        }
moel@150
    41
        s.Append("Common Language Runtime: "); 
moel@150
    42
        s.AppendLine(Environment.Version.ToString());
moel@150
    43
        s.Append("Operating System: ");
moel@150
    44
        s.AppendLine(Environment.OSVersion.ToString());
moel@150
    45
        s.Append("Process Type: ");
moel@150
    46
        s.AppendLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
moel@150
    47
        reportTextBox.Text = s.ToString();        
moel@150
    48
      }
moel@150
    49
    }
moel@150
    50
moel@150
    51
    private void sendButton_Click(object sender, EventArgs e) {
moel@150
    52
      try {
moel@150
    53
        Version version = typeof(CrashForm).Assembly.GetName().Version;
moel@150
    54
        WebRequest request = WebRequest.Create(
moel@150
    55
          "http://openhardwaremonitor.org/report.php");
moel@150
    56
        request.Method = "POST";
moel@150
    57
        request.Timeout = 5000;
moel@150
    58
        request.ContentType = "application/x-www-form-urlencoded";
moel@150
    59
moel@150
    60
        string report =
moel@206
    61
          "type=crash&" +
moel@206
    62
          "version=" + Uri.EscapeDataString(version.ToString()) + "&" +
moel@206
    63
          "report=" + Uri.EscapeDataString(reportTextBox.Text) + "&" +
moel@206
    64
          "comment=" + Uri.EscapeDataString(commentTextBox.Text) + "&" +
moel@206
    65
          "email=" + Uri.EscapeDataString(emailTextBox.Text);
moel@150
    66
        byte[] byteArray = Encoding.UTF8.GetBytes(report);
moel@150
    67
        request.ContentLength = byteArray.Length;
moel@150
    68
moel@150
    69
        try {
moel@150
    70
          Stream dataStream = request.GetRequestStream();
moel@150
    71
          dataStream.Write(byteArray, 0, byteArray.Length);
moel@150
    72
          dataStream.Close();
moel@150
    73
moel@150
    74
          WebResponse response = request.GetResponse();
moel@150
    75
          dataStream = response.GetResponseStream();
moel@150
    76
          StreamReader reader = new StreamReader(dataStream);
moel@150
    77
          string responseFromServer = reader.ReadToEnd();
moel@150
    78
          reader.Close();
moel@150
    79
          dataStream.Close();
moel@150
    80
          response.Close();
moel@150
    81
moel@150
    82
          Close();
moel@150
    83
        } catch (WebException) {
moel@150
    84
          MessageBox.Show("Sending the crash report failed.", "Error", 
moel@150
    85
            MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@150
    86
        }
moel@150
    87
      } catch {
moel@150
    88
      }
moel@150
    89
    }
moel@150
    90
  }  
moel@150
    91
}