|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Windows; |
| 4 | +using System.Windows.Controls; |
| 5 | + |
| 6 | +namespace Advanced_PortChecker.Classes |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// A static helper class to export items to the disk. |
| 10 | + /// </summary> |
| 11 | + internal static class ExportWriter |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Export the listview items in plain text format to the drive. |
| 15 | + /// </summary> |
| 16 | + /// <param name="path">The path where the export list should be saved.</param> |
| 17 | + /// <param name="lvPorts">The listview control containing all the LvCheck items.</param> |
| 18 | + internal static void SaveAsText(string path, ItemsControl lvPorts) |
| 19 | + { |
| 20 | + if (lvPorts.Items.Count == 0) return; |
| 21 | + |
| 22 | + try |
| 23 | + { |
| 24 | + using (StreamWriter sw = new StreamWriter(path)) |
| 25 | + { |
| 26 | + sw.WriteLine("Advanced PortChecker - " + DateTime.Now); |
| 27 | + for (int i = 0; i < lvPorts.Items.Count; i++) |
| 28 | + { |
| 29 | + LvCheck l = (LvCheck)lvPorts.Items[i]; |
| 30 | + if (i == lvPorts.Items.Count - 1) |
| 31 | + { |
| 32 | + sw.Write(l.Address + "\t" + l.Port + "\t" + l.Type + "\t" + l.Description); |
| 33 | + } |
| 34 | + else |
| 35 | + { |
| 36 | + sw.WriteLine(l.Address + "\t" + l.Port + "\t" + l.Type + "\t" + l.Description); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + MessageBox.Show("Successfully exported all items!", "Advanced PortChecker", MessageBoxButton.OK, MessageBoxImage.Information); |
| 42 | + } |
| 43 | + catch (Exception ex) |
| 44 | + { |
| 45 | + MessageBox.Show(ex.Message, "Advanced PortChecker", MessageBoxButton.OK, MessageBoxImage.Error); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // ReSharper disable once InconsistentNaming |
| 50 | + /// <summary> |
| 51 | + /// Export the listview items in HTML format to the drive. |
| 52 | + /// </summary> |
| 53 | + /// <param name="path">The path where the export list should be saved.</param> |
| 54 | + /// <param name="lvPorts">The listview control containing all the LvCheck items.</param> |
| 55 | + internal static void SaveAsHTML(string path, ItemsControl lvPorts) |
| 56 | + { |
| 57 | + if (lvPorts.Items.Count == 0) return; |
| 58 | + |
| 59 | + try |
| 60 | + { |
| 61 | + using (StreamWriter sw = new StreamWriter(path)) |
| 62 | + { |
| 63 | + sw.WriteLine("<HTML>"); |
| 64 | + sw.WriteLine("<head>"); |
| 65 | + sw.WriteLine("<title>Advanced PortChecker - " + DateTime.Now + "</title>"); |
| 66 | + sw.WriteLine("</head>"); |
| 67 | + |
| 68 | + sw.WriteLine("<body>"); |
| 69 | + |
| 70 | + sw.WriteLine("<h1>Export list</h1>"); |
| 71 | + sw.WriteLine("<table border='1'>"); |
| 72 | + sw.WriteLine("<tr><th>Address</th><th>Port</th><th>Type</th><th>Description</th></tr>"); |
| 73 | + foreach (LvCheck l in lvPorts.Items) |
| 74 | + { |
| 75 | + sw.WriteLine("<tr><td>" + l.Address + "</td><td>" + l.Port + "</td><td>" + l.Type + "</td><td>" + l.Description + "</td></tr>"); |
| 76 | + } |
| 77 | + sw.WriteLine("</table>"); |
| 78 | + |
| 79 | + sw.WriteLine("</body>"); |
| 80 | + sw.Write("</HTML>"); |
| 81 | + } |
| 82 | + |
| 83 | + MessageBox.Show("Successfully exported all items!", "Advanced PortChecker", MessageBoxButton.OK, MessageBoxImage.Information); |
| 84 | + } |
| 85 | + catch (Exception ex) |
| 86 | + { |
| 87 | + MessageBox.Show(ex.Message, "Advanced PortChecker", MessageBoxButton.OK, MessageBoxImage.Error); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // ReSharper disable once InconsistentNaming |
| 92 | + /// <summary> |
| 93 | + /// Export the listview items in CSV format to the drive. |
| 94 | + /// </summary> |
| 95 | + /// <param name="path">The path where the export list should be saved.</param> |
| 96 | + /// <param name="lvPorts">The listview control containing all the LvCheck items.</param> |
| 97 | + internal static void SaveAsCSV(string path, ItemsControl lvPorts) |
| 98 | + { |
| 99 | + try |
| 100 | + { |
| 101 | + using (StreamWriter sw = new StreamWriter(path)) |
| 102 | + { |
| 103 | + sw.WriteLine("Advanced PortChecker;" + DateTime.Now); |
| 104 | + for (int i = 0; i < lvPorts.Items.Count; i++) |
| 105 | + { |
| 106 | + LvCheck l = (LvCheck)lvPorts.Items[i]; |
| 107 | + if (i == lvPorts.Items.Count - 1) |
| 108 | + { |
| 109 | + sw.Write(l.Address + ";" + l.Port + ";" + l.Type + ";" + l.Description); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + sw.WriteLine(l.Address + ";" + l.Port + ";" + l.Type + ";" + l.Description); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + MessageBox.Show("Successfully exported all items!", "Advanced PortChecker", MessageBoxButton.OK, MessageBoxImage.Information); |
| 119 | + } |
| 120 | + catch (Exception ex) |
| 121 | + { |
| 122 | + MessageBox.Show(ex.Message, "Advanced PortChecker", MessageBoxButton.OK, MessageBoxImage.Error); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments