public static bool ExportAsHtml(WipeReport report, string path, string style)
        {
            StreamWriter writer = null;

            try {
                writer = new StreamWriter(path);

                if(writer.BaseStream.CanWrite == false) {
                    Debug.ReportWarning("Cannot write to file {0}", path);
                    return false;
                }

                writer.Write(GetHtmlString(report, style));
                return true;
            }
            catch(Exception e) {
                Debug.ReportWarning("Cannot write to file {0}. Exception: {0}", e.Message);
                return false;
            }
            finally {
                if(writer != null && writer.BaseStream.CanWrite) {
                    writer.Close();
                }
            }
        }
        private void button2_Click_1(object sender, EventArgs e)
        {
            // generate a test report
            WipeReport r = new WipeReport();
            r.Statistics = new WipeStatistics();

            for(int i = 0; i < 3; i++) {
                r.Errors.Add(new WipeError(DateTime.Now, ErrorSeverity.High, "Test"));
                r.Errors.Add(new WipeError(DateTime.Now, ErrorSeverity.Low, "Test"));
                r.Errors.Add(new WipeError(DateTime.Now, ErrorSeverity.Medium, "Test"));
                r.FailedObjects.Add(new FailedObject(WipeObjectType.File, "C:\\test.txt", null));
            }

            r.FailedObjects[0].AssociatedError = r.Errors[0];
            r.FailedObjects[1].AssociatedError = r.Errors[1];
            r.FailedObjects[2].AssociatedError = r.Errors[2];

            // load the style
            string style = "";

            try {
                if(DefaultStyleRadioButton.Checked) {
                    style = WebReportStyle.HTMLReportStyle;
                }
                else {
                    // load from file
                    string path = StyleTextBox.Text;

                    if(File.Exists(path) == false) {
                        MessageBox.Show("Style file could not be found.", "SecureDelete",
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    style = File.ReadAllText(path);
                }

                // generate the report
                string reportPath = Path.GetTempPath() + Path.GetRandomFileName() + ".htm";
                ReportExporter.ExportAsHtml(r, reportPath, style);
                System.Diagnostics.Process.Start(reportPath);
            }
            catch(Exception ex) {
                Debug.ReportError("Failed to generate test report. Exception: {0}", ex.Message);
            }
        }
        public static void ExportAsHtml(WipeReport report, string path, string style)
        {
            try {
                StreamWriter writer = new StreamWriter(path);

                if(writer.BaseStream.CanWrite == false) {
                    Debug.ReportWarning("Cannot write to file {0}", path);
                }

                // write html file structure
                writer.WriteLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
                writer.WriteLine("<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\">");
                writer.WriteLine("<head>");
                writer.WriteLine("<meta http-equiv=\"Content-Language\" content=\"en-us\" />");
                writer.WriteLine("<meta http-equiv=\"Content-Type\\ content=\"text/html; charset=utf-8\" />");
                writer.WriteLine("<title>Wipe Report</title>");

                // load styles from resource
                writer.WriteLine("<style type=\"text/css\">");
                writer.WriteLine(style);
                writer.WriteLine("</style>");

                writer.WriteLine("</head>");
                writer.WriteLine("<body class=\"BodyStyle\">");
                writer.WriteLine("<p name=\"#top\" class=\"Header\">Wipe Report</p>");
                writer.Write("<p class=\"Subheader\">Report created on " + DateTime.Now.ToString());
                writer.WriteLine("</p>");

                // write statistics
                if(report.Statistics != null) {
                    writer.WriteLine("<p class=\"Subtitle\">Statistics</p>");
                    writer.WriteLine("<p class=\"Normal\">");

                    writer.Write("Start Time: "); writer.Write(report.Statistics.StartTime.ToString()); writer.WriteLine("<br/>");
                    writer.Write("End Time: "); writer.Write(report.Statistics.EndTime.ToString()); writer.WriteLine("<br/>");
                    writer.Write("Duration: "); writer.Write(report.Statistics.Duration.ToString()); writer.WriteLine("<br/>");
                    writer.Write("Failed Objects: "); writer.Write(report.Statistics.FailedObjects); writer.WriteLine("<br/>");
                    writer.Write("Errors: "); writer.Write(report.Statistics.Errors); writer.WriteLine("<br/>");
                    writer.Write("Average Write Speed: "); writer.Write(report.Statistics.AverageWriteSpeed); writer.WriteLine("<br/>");
                    writer.Write("Total Wiped Bytes: "); writer.Write(report.Statistics.TotalWipedBytes); writer.WriteLine("<br/>");
                    writer.Write("Bytes In Cluster Tips: "); writer.Write(report.Statistics.BytesInClusterTips); writer.WriteLine("<br/>");

                    writer.WriteLine("</p>");
                }

                // failed objects
                writer.WriteLine("<p class=\"Subtitle\">" + report.FailedObjects.Count.ToString() + " Failed objects</p>");

                if(report.FailedObjects.Count > 0) {
                    // create table
                    writer.WriteLine("<table style=\"width: 100%\" class=\"TableStyle\"");

                    // create columns
                    writer.WriteLine("<tr>");
                    writer.WriteLine("<td class=\"TabelHeader2\" style=\"width: 50px\">ID</td>");
                    writer.WriteLine("<td class=\"TabelHeader\" style=\"width: 120px\">Type</td>");
                    writer.WriteLine("<td class=\"TabelHeader\">Path</td>");
                    writer.WriteLine("<td class=\"TabelHeader2\" style=\"width: 160px\">Associated error</td>");
                    writer.WriteLine("</tr>");

                    // write failed objects
                    for(int i = 0; i < report.FailedObjects.Count; i++) {
                        writer.WriteLine("<tr>");
                        writer.WriteLine("<td class=\"Normal2\">" + ((int)(i + 1)).ToString() + "</td>");
                        writer.WriteLine("<td class=\"Normal\">" + report.FailedObjects[i].Type.ToString() + "</td>");
                        writer.WriteLine("<td class=\"Normal\">" + report.FailedObjects[i].Path + "</td>");

                        // associated error <a href="#test">
                        if(report.FailedObjects[i].AssociatedError != null) {
                            // find the indes
                            int index = report.Errors.IndexOf(report.FailedObjects[i].AssociatedError) + 1;

                            writer.WriteLine("<td class=\"Normal2\"><a href=\"#" + index.ToString() + "\"> #" + index.ToString() + "</a></td>");
                        }
                        else {
                            writer.WriteLine("<td class=\"Normal\">-</td>");
                        }

                        writer.WriteLine("</tr>");
                    }

                    writer.WriteLine("</table>");
                    writer.WriteLine("<p class=\"NavigatorStyle\"><a class=\"NavigatorStyle\" href=\"#top\">Top</a></p>");
                }

                // errors
                writer.WriteLine("<p class=\"Subtitle\">" + report.Errors.Count.ToString() + " Errors</p>");

                if(report.Errors.Count > 0) {
                    // create table
                    writer.WriteLine("<table style=\"width: 100%\" class=\"TableStyle\"");

                    // create columns
                    writer.WriteLine("<tr>");
                    writer.WriteLine("<td class=\"TabelHeader2\" style=\"width: 50px\">ID</td>");
                    writer.WriteLine("<td class=\"TabelHeader\" style=\"width: 120px\">Time</td>");
                    writer.WriteLine("<td class=\"TabelHeader\" style=\"width: 120px\">Severity</td>");
                    writer.WriteLine("<td class=\"TabelHeader\">Message</td>");
                    writer.WriteLine("</tr>");

                    // write failed objects
                    for(int i = 0; i < report.Errors.Count; i++) {
                        writer.WriteLine("<tr>");
                        writer.Write("<td class=\"");

                        switch(report.Errors[i].Severity) {
                            case ErrorSeverity.High: {
                                writer.Write("HighSeverity");
                                break;
                            }
                            case ErrorSeverity.Medium: {
                                writer.Write("MediumSeverity");
                                break;
                            }
                            case ErrorSeverity.Low: {
                                writer.Write("LowSeverity");
                                break;
                            }
                        }

                        writer.Write("\">");

                        int number = i + 1;
                        writer.Write("<a name=\"#" + number.ToString() + "\">");
                        writer.WriteLine(number.ToString() + "</a></td>");
                        writer.WriteLine("<td class=\"Normal\">" + report.Errors[i].Time.ToLongTimeString() + "</td>");
                        writer.WriteLine("<td class=\"Normal\">" + report.Errors[i].Severity.ToString() + "</td>");
                        writer.WriteLine("<td class=\"Normal\">" + report.Errors[i].Message.ToString() + "</td>");
                        writer.WriteLine("</tr>");
                    }

                    writer.WriteLine("</table>");
                    writer.WriteLine("<p class=\"NavigatorStyle\"><a class=\"NavigatorStyle\" href=\"#top\">Top</a></p>");
                }

                // footer
                writer.WriteLine("<p class=\"Normal\"></br></br></p>");
                writer.WriteLine("<p class=\"Footer\">Generated by SecureDelete. Copyright &copy 2008 <a href=\"mailto:[email protected]\">Lup Gratian</a></p>");

                // end of document
                writer.WriteLine("</body>");
                writer.WriteLine("</html>");
                writer.Close();
            }
            catch(Exception e) {
                Debug.ReportWarning("Cannot write to file {0}. Exception: {0}", e.Message);
            }
        }
        public static void ExportAsXml(WipeReport report, string path)
        {
            try {
                StreamWriter writer = new StreamWriter(path);

                if(writer.BaseStream.CanWrite == false) {
                    Debug.ReportWarning("Cannot write to file {0}", path);
                }

                // write xml file structure
                writer.WriteLine("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>");
                writer.WriteLine("<WipeReport>");

                // basic info
                if(report.Statistics != null) {
                    writer.WriteLine("<Statistics>");
                    writer.Write("<StartTime>"); writer.Write(report.Statistics.StartTime.ToString()); writer.WriteLine("</StartTime>");
                    writer.Write("<EndTime>"); writer.Write(report.Statistics.EndTime.ToString()); writer.WriteLine("</EndTime>");
                    writer.Write("<Duration>"); writer.Write(report.Statistics.Duration.ToString()); writer.WriteLine("</Duration>");
                    writer.Write("<FailedObjects>"); writer.Write(report.Statistics.FailedObjects); writer.WriteLine("</FailedObjects>");
                    writer.Write("<Errors>"); writer.Write(report.Statistics.Errors); writer.WriteLine("</Errors>");
                    writer.Write("<AverageWriteSpeed>"); writer.Write(report.Statistics.AverageWriteSpeed); writer.WriteLine("</AverageWriteSpeed>");
                    writer.Write("<TotalWipedBytes>"); writer.Write(report.Statistics.TotalWipedBytes); writer.WriteLine("</TotalWipedBytes>");
                    writer.Write("<BytesInClusterTips>"); writer.Write(report.Statistics.BytesInClusterTips); writer.WriteLine("</BytesInClusterTips>");
                    writer.WriteLine("</Statistics>");
                }

                // failed objects
                writer.WriteLine("<FailedObjects>");
                for(int i = 0; i < report.FailedObjects.Count; i++) {
                    writer.WriteLine("<Object>");
                    writer.Write("<Type>"); writer.Write(report.FailedObjects[i].Type.ToString()); writer.WriteLine("</Type>");
                    writer.Write("<Path>"); writer.Write(report.FailedObjects[i].Path); writer.WriteLine("</Path>");
                    writer.WriteLine("</Object>");
                }
                writer.WriteLine("</FailedObjects>");

                // errors
                writer.WriteLine("<Errors>");
                for(int i = 0; i < report.Errors.Count; i++) {
                    writer.WriteLine("<Error>");
                    writer.Write("<Time>"); writer.Write(report.Errors[i].Time.ToString()); writer.WriteLine("</Time>");
                    writer.Write("<Severity>"); writer.Write(report.Errors[i].Severity.ToString()); writer.WriteLine("</Severity>");
                    writer.Write("<Message>"); writer.Write(report.Errors[i].Message); writer.WriteLine("</Message>");
                    writer.WriteLine("</Error>");
                }
                writer.WriteLine("</Errors>");

                // end of document
                writer.WriteLine("</WipeReport>");

                writer.Close();
            }
            catch(Exception e) {
                Debug.ReportWarning("Cannot write to file {0}. Exception: {0}", e.Message);
            }
        }
        public static void ExportAsText(WipeReport report, string path)
        {
            try {
                StreamWriter writer = new StreamWriter(path);

                if(writer.BaseStream.CanWrite == false) {
                    Debug.ReportWarning("Cannot write to file {0}", path);
                }

                // header
                writer.WriteLine("SecureDelete Wipe Report\r\n");

                // statistics
                if(report.Statistics != null) {
                    writer.WriteLine("Statistics");
                    writer.WriteLine("------------------------\r\n");

                    writer.Write("Start Time: "); writer.WriteLine(report.Statistics.StartTime.ToString());
                    writer.Write("End Time: "); writer.WriteLine(report.Statistics.EndTime.ToString());
                    writer.Write("Duration: "); writer.WriteLine(report.Statistics.Duration.ToString());
                    writer.Write("Failed Objects: "); writer.WriteLine(report.Statistics.FailedObjects);
                    writer.Write("Errors: "); writer.WriteLine(report.Statistics.Errors);
                    writer.Write("Average Write Speed: "); writer.WriteLine(report.Statistics.AverageWriteSpeed);
                    writer.Write("Total Wiped Bytes: "); writer.WriteLine(report.Statistics.TotalWipedBytes);
                    writer.Write("Bytes In Cluster Tips: "); writer.WriteLine(report.Statistics.BytesInClusterTips);

                    writer.Write("\r\n");
                }

                // failed objects
                writer.WriteLine("Failed Objects - " + report.FailedObjects.Count.ToString());
                writer.WriteLine("------------------------");

                for(int i = 0; i < report.FailedObjects.Count; i++) {
                    writer.WriteLine("\r\n#{0}", i);
                    writer.Write("Type: "); writer.WriteLine(report.FailedObjects[i].Type.ToString());
                    writer.Write("Path: "); writer.WriteLine(report.FailedObjects[i].Path);
                }

                // errors
                writer.WriteLine("Errors - " + report.Errors.Count.ToString());
                writer.WriteLine("------------------------");

                for(int i = 0; i < report.Errors.Count; i++) {
                    writer.WriteLine("\r\n#{0}", i);
                    writer.Write("Time: "); writer.WriteLine(report.Errors[i].Time.ToString());
                    writer.Write("Severity: "); writer.WriteLine(report.Errors[i].Severity.ToString());
                    writer.Write("Message: "); writer.WriteLine(report.Errors[i].Message);
                }

                writer.Close();
            }
            catch(Exception e) {
                Debug.ReportWarning("Cannot write to file {0}. Exception: {0}", e.Message);
            }
        }
        private void TestButton_Click(object sender, EventArgs e)
        {
            // create a test report
            WipeReport report = new WipeReport();

            report.Statistics = new WipeStatistics();
            report.Errors.Add(new WipeError(DateTime.Now, ErrorSeverity.High, "Test message"));
            report.Errors.Add(new WipeError(DateTime.Now, ErrorSeverity.Medium, "Test message"));
            report.Errors.Add(new WipeError(DateTime.Now, ErrorSeverity.Low, "Test message"));

            if(_action.Start() == false) {
                MessageBox.Show("Failed to send test mail message.", "SecureDelete",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else {
                MessageBox.Show("Test mail message successfully sent.", "SecureDelete",
                                MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
 public void DisposeTool()
 {
     _report = null;
     FailedListView.Items.Clear();
     ErrorListView.Items.Clear();
     StatisticsBox.Text = string.Empty;
 }
        /// <summary>
        /// Generate a report containing statistics, failed objects and errors
        /// </summary>
        /// <remarks>This method is not intended to be used in conjunction with scheduled tasks.</remarks>
        public WipeReport GenerateReport()
        {
            // check if wipe is stopped
            if(status != SessionStatus.Stopped) {
                Debug.ReportWarning("Report requested while not in stop state. Status: {0}", status);
                return null;
            }

            WipeReport report = new WipeReport(guid);
            List<WipeError> errors = null;
            report.Statistics = (WipeStatistics)statistics.Clone();
            report.Statistics.Errors += _beforeWipeErrors.Count + _afterWipeErrors.Count;

            // add the failed objects without their associated errors
            try {
                FailedObject[] failed = GetFailedObjects(true);

                if(failed != null && failed.Length > 0) {
                    report.FailedObjects.AddRange(failed);
                }
            }
            catch { }

            // add errors
            errors = GetWipeErrors();

            if(errors != null) {
                report.Errors.AddRange(errors);
            }

            return report;
        }