|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using System.Text; |
5 | | -using System.Threading.Tasks; |
| 3 | +using System.IO; |
6 | 4 |
|
7 | 5 | namespace MemPlus.Business.PROCESS |
8 | 6 | { |
| 7 | + /// <summary> |
| 8 | + /// Static class that can be used to export ProcessDetail information |
| 9 | + /// </summary> |
9 | 10 | internal static class ProcessDetailExporter |
10 | 11 | { |
| 12 | + /// <summary> |
| 13 | + /// Export data to a specific path |
| 14 | + /// </summary> |
| 15 | + /// <param name="path">The path where the data should be stored</param> |
| 16 | + /// <param name="data">The data that should be exported</param> |
| 17 | + private static void Export(string path, string data) |
| 18 | + { |
| 19 | + using (StreamWriter sw = new StreamWriter(path)) |
| 20 | + { |
| 21 | + sw.Write(data); |
| 22 | + } |
| 23 | + } |
11 | 24 |
|
| 25 | + /// <summary> |
| 26 | + /// Export a list of ProcessDetail objects to a specific path in text format |
| 27 | + /// </summary> |
| 28 | + /// <param name="path">The path where the data should be stored</param> |
| 29 | + /// <param name="processDetails">The list of ProcessDetail objects that need to be exported</param> |
| 30 | + internal static void ExportText(string path, List<ProcessDetail> processDetails) |
| 31 | + { |
| 32 | + if (processDetails == null || processDetails.Count == 0) throw new ArgumentNullException(); |
| 33 | + |
| 34 | + string exportData = "MemPlus - Process Analyzer Data (" + DateTime.Now + ")"; |
| 35 | + exportData += Environment.NewLine; |
| 36 | + exportData += "---"; |
| 37 | + exportData += Environment.NewLine; |
| 38 | + |
| 39 | + for (int i = 0; i < processDetails.Count; i++) |
| 40 | + { |
| 41 | + ProcessDetail pd = processDetails[i]; |
| 42 | + exportData += pd.ProcessId + "\t" + pd.ProcessName + "\t" + pd.ProcessLocation + "\t" + pd.MemoryUsage; |
| 43 | + |
| 44 | + if (i != processDetails.Count - 1) |
| 45 | + { |
| 46 | + exportData += Environment.NewLine; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + Export(path, exportData); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Export a list of ProcessDetail objects to a specific path in HTML format |
| 55 | + /// </summary> |
| 56 | + /// <param name="path">The path where the data should be stored</param> |
| 57 | + /// <param name="processDetails">The list of ProcessDetail objects that need to be exported</param> |
| 58 | + internal static void ExportHtml(string path, List<ProcessDetail> processDetails) |
| 59 | + { |
| 60 | + if (processDetails == null || processDetails.Count == 0) throw new ArgumentNullException(); |
| 61 | + |
| 62 | + string exportData = "<html>"; |
| 63 | + |
| 64 | + exportData += "<head>"; |
| 65 | + exportData += "<title>MemPlus - Process Analyzer Data</title>"; |
| 66 | + exportData += "</head>"; |
| 67 | + |
| 68 | + exportData += "<body>"; |
| 69 | + exportData += "<h1>MemPlus - Process Analyzer Data (" + DateTime.Now + ")</h1>"; |
| 70 | + |
| 71 | + exportData += "<table border=\"1\">"; |
| 72 | + exportData += "<thead>"; |
| 73 | + exportData += "<tr><th>Process ID</th><th>Process name</th><th>Process location</th><th>Memory usage</th></tr>"; |
| 74 | + exportData += "</thead>"; |
| 75 | + exportData += "<tbody>"; |
| 76 | + |
| 77 | + foreach (ProcessDetail pd in processDetails) |
| 78 | + { |
| 79 | + exportData += "<tr>"; |
| 80 | + exportData += "<td>" + pd.ProcessId + "</td>"; |
| 81 | + exportData += "<td>" + pd.ProcessName + "</td>"; |
| 82 | + exportData += "<td>" + pd.ProcessLocation + "</td>"; |
| 83 | + exportData += "<td>" + pd.MemoryUsage + "</td>"; |
| 84 | + } |
| 85 | + |
| 86 | + exportData += "</tbody>"; |
| 87 | + exportData += "</table>"; |
| 88 | + exportData += "</body>"; |
| 89 | + exportData += "</html>"; |
| 90 | + |
| 91 | + Export(path, exportData); |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// Export a list of ProcessDetail objects to a specific path in CSV format |
| 96 | + /// </summary> |
| 97 | + /// <param name="path">The path where the data should be stored</param> |
| 98 | + /// <param name="processDetails">The list of ProcessDetail objects that need to be exported</param> |
| 99 | + internal static void ExportCsv(string path, List<ProcessDetail> processDetails) |
| 100 | + { |
| 101 | + ExportDelimiter(path, ",", processDetails); |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Export a list of ProcessDetail objects to a specific path in Excel format |
| 106 | + /// </summary> |
| 107 | + /// <param name="path">The path where the data should be stored</param> |
| 108 | + /// <param name="processDetails">The list of ProcessDetail objects that need to be exported</param> |
| 109 | + internal static void ExportExcel(string path, List<ProcessDetail> processDetails) |
| 110 | + { |
| 111 | + ExportDelimiter(path, ";", processDetails); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Export a list of ProcessDetail objects using a specific delimiter character |
| 116 | + /// </summary> |
| 117 | + /// <param name="path">The path where the data should be stored</param> |
| 118 | + /// <param name="delimiter">The delimiter that should be used to split the data</param> |
| 119 | + /// <param name="processDetails">The list of ProcessDetail objects that need to be exported</param> |
| 120 | + private static void ExportDelimiter(string path, string delimiter, IReadOnlyList<ProcessDetail> processDetails) |
| 121 | + { |
| 122 | + if (processDetails == null || processDetails.Count == 0) throw new ArgumentNullException(); |
| 123 | + |
| 124 | + string exportData = "Process ID" + delimiter + "Process name" + delimiter + "Process location" + delimiter + "Memory usage"; |
| 125 | + exportData += Environment.NewLine; |
| 126 | + |
| 127 | + for (int i = 0; i < processDetails.Count; i++) |
| 128 | + { |
| 129 | + exportData += processDetails[i].ProcessId + delimiter + processDetails[i].ProcessName + delimiter + |
| 130 | + processDetails[i].ProcessLocation + delimiter + processDetails[i].MemoryUsage; |
| 131 | + |
| 132 | + if (i != processDetails.Count - 1) |
| 133 | + { |
| 134 | + exportData += Environment.NewLine; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + Export(path, exportData); |
| 139 | + } |
12 | 140 | } |
13 | 141 | } |
0 commit comments