Skip to content

Commit e020c2f

Browse files
committed
Chore: Improve csv export
1 parent eac4754 commit e020c2f

File tree

7 files changed

+37
-13
lines changed

7 files changed

+37
-13
lines changed

Source/NETworkManager.Models/Export/ExportManager.ConnectionInfo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Xml.Linq;
77
using NETworkManager.Models.Network;
8+
using NETworkManager.Utilities;
89
using Newtonsoft.Json;
910

1011
namespace NETworkManager.Models.Export;
@@ -51,7 +52,7 @@ private static void CreateCsv(IEnumerable<ConnectionInfo> collection, string fil
5152

5253
foreach (var info in collection)
5354
stringBuilder.AppendLine(
54-
$"{info.Protocol},{info.LocalIPAddress},{info.LocalPort},{info.RemoteIPAddress},{info.RemotePort},{info.RemoteHostname},{info.TcpState},{info.ProcessId},{info.ProcessName},{info.ProcessPath}");
55+
$"{info.Protocol},{info.LocalIPAddress},{info.LocalPort},{info.RemoteIPAddress},{info.RemotePort},{CsvHelper.QuoteString(info.RemoteHostname)},{info.TcpState},{info.ProcessId},{CsvHelper.QuoteString(info.ProcessName)},{CsvHelper.QuoteString(info.ProcessPath)}");
5556

5657
File.WriteAllText(filePath, stringBuilder.ToString());
5758
}

Source/NETworkManager.Models/Export/ExportManager.IPScannerHostInfo.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,25 @@ private static void CreateCsv(IEnumerable<IPScannerHostInfo> collection, string
8989
stringBuilder.AppendLine(
9090
$"{info.IsReachable}," +
9191
$"{info.PingInfo.IPAddress}," +
92-
$"{info.Hostname}," +
92+
$"{CsvHelper.QuoteString(info.Hostname)}," +
9393
$"{info.PingInfo.Status}," +
9494
$"{DateTimeHelper.DateTimeToFullDateTimeString(info.PingInfo.Timestamp)}," +
9595
$"{Ping.TimeToString(info.PingInfo.Status, info.PingInfo.Time, true)}," +
9696
$"{info.PingInfo.TTL}," +
9797
$"{info.PingInfo.Bytes}," +
9898
$"{(info.IsAnyPortOpen ? PortState.Open : PortState.Closed)}," +
99-
$"\"{stringBuilderPorts.ToString().TrimEnd(';')}\"," +
99+
$"{CsvHelper.QuoteString(stringBuilderPorts.ToString().TrimEnd(';'))}," +
100100
$"{info.NetBIOSInfo?.IsReachable}," +
101101
$"{info.NetBIOSInfo?.IPAddress}," +
102-
$"{info.NetBIOSInfo?.ComputerName}," +
103-
$"{info.NetBIOSInfo?.UserName}," +
104-
$"{info.NetBIOSInfo?.GroupName}," +
102+
$"{CsvHelper.QuoteString(info.NetBIOSInfo?.ComputerName)}," +
103+
$"{CsvHelper.QuoteString(info.NetBIOSInfo?.UserName)}," +
104+
$"{CsvHelper.QuoteString(info.NetBIOSInfo?.GroupName)}," +
105105
$"{info.NetBIOSInfo?.MACAddress}," +
106-
$"{info.NetBIOSInfo?.Vendor}," +
106+
$"{CsvHelper.QuoteString(info.NetBIOSInfo?.Vendor)}," +
107107
$"{info.MACAddress}," +
108-
$"\"{info.Vendor}\"," +
108+
$"{CsvHelper.QuoteString(info.Vendor)}," +
109109
$"{info.ARPMACAddress}," +
110-
$"\"{info.ARPVendor}\""
110+
$"{CsvHelper.QuoteString(info.ARPVendor)}"
111111
);
112112
}
113113

Source/NETworkManager.Models/Export/ExportManager.OUIInfo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Xml.Linq;
77
using NETworkManager.Models.Lookup;
8+
using NETworkManager.Utilities;
89
using Newtonsoft.Json;
910

1011
namespace NETworkManager.Models.Export;
@@ -48,7 +49,7 @@ private static void CreateCsv(IEnumerable<OUIInfo> collection, string filePath)
4849
stringBuilder.AppendLine($"{nameof(OUIInfo.MACAddress)},{nameof(OUIInfo.Vendor)}");
4950

5051
foreach (var info in collection)
51-
stringBuilder.AppendLine($"{info.MACAddress},\"{info.Vendor}\"");
52+
stringBuilder.AppendLine($"{info.MACAddress},{CsvHelper.QuoteString(info.Vendor)}");
5253

5354
File.WriteAllText(filePath, stringBuilder.ToString());
5455
}

Source/NETworkManager.Models/Export/ExportManager.PortLookupInfo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Xml.Linq;
77
using NETworkManager.Models.Lookup;
8+
using NETworkManager.Utilities;
89
using Newtonsoft.Json;
910

1011
namespace NETworkManager.Models.Export;
@@ -49,7 +50,7 @@ private static void CreateCsv(IEnumerable<PortLookupInfo> collection, string fil
4950
$"{nameof(PortLookupInfo.Number)},{nameof(PortLookupInfo.Protocol)},{nameof(PortLookupInfo.Service)},{nameof(PortLookupInfo.Description)}");
5051

5152
foreach (var info in collection)
52-
stringBuilder.AppendLine($"{info.Number},{info.Protocol},{info.Service},\"{info.Description}\"");
53+
stringBuilder.AppendLine($"{info.Number},{info.Protocol},{CsvHelper.QuoteString(info.Service)},{CsvHelper.QuoteString(info.Description)}");
5354

5455
File.WriteAllText(filePath, stringBuilder.ToString());
5556
}

Source/NETworkManager.Models/Export/ExportManager.PortScannerInfo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Xml.Linq;
77
using NETworkManager.Models.Lookup;
88
using NETworkManager.Models.Network;
9+
using NETworkManager.Utilities;
910
using Newtonsoft.Json;
1011

1112
namespace NETworkManager.Models.Export;
@@ -52,7 +53,7 @@ private static void CreateCsv(IEnumerable<PortScannerPortInfo> collection, strin
5253

5354
foreach (var info in collection)
5455
stringBuilder.AppendLine(
55-
$"{info.IPAddress},{info.Hostname},{info.Port},{info.LookupInfo.Protocol},{info.LookupInfo.Service},\"{info.LookupInfo.Description}\",{info.State}");
56+
$"{info.IPAddress},{CsvHelper.QuoteString(info.Hostname)},{info.Port},{info.LookupInfo.Protocol},{CsvHelper.QuoteString(info.LookupInfo.Service)},{CsvHelper.QuoteString(info.LookupInfo.Description)},{info.State}");
5657

5758
File.WriteAllText(filePath, stringBuilder.ToString());
5859
}

Source/NETworkManager.Models/Export/ExportManager.TracerouteHopInfo.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Xml.Linq;
77
using NETworkManager.Models.Network;
8+
using NETworkManager.Utilities;
89
using Newtonsoft.Json;
910

1011
namespace NETworkManager.Models.Export;
@@ -51,7 +52,11 @@ private static void CreateCsv(IEnumerable<TracerouteHopInfo> collection, string
5152

5253
foreach (var info in collection)
5354
stringBuilder.AppendLine(
54-
$"{info.Hop},{info.Status1},{Ping.TimeToString(info.Status1, info.Time1, true)},{info.Status2},{Ping.TimeToString(info.Status2, info.Time2, true)},{info.Status3},{Ping.TimeToString(info.Status3, info.Time3, true)},{info.IPAddress},{info.Hostname},{info.IPGeolocationResult?.Info?.Continent},{info.IPGeolocationResult?.Info?.Country},{info.IPGeolocationResult?.Info?.Region},{info.IPGeolocationResult?.Info?.City},{info.IPGeolocationResult?.Info?.District},{info.IPGeolocationResult?.Info?.Isp?.Replace(",", "")},{info.IPGeolocationResult?.Info?.Org?.Replace(",", "")},{info.IPGeolocationResult?.Info?.As?.Replace(",", "")},{info.IPGeolocationResult?.Info?.Asname?.Replace(",", "")},{info.IPGeolocationResult?.Info?.Hosting},{info.IPGeolocationResult?.Info?.Proxy},{info.IPGeolocationResult?.Info?.Mobile}");
55+
$"{info.Hop},{info.Status1},{Ping.TimeToString(info.Status1, info.Time1, true)},{info.Status2},{Ping.TimeToString(info.Status2, info.Time2, true)},{info.Status3},{Ping.TimeToString(info.Status3, info.Time3, true)},{info.IPAddress}," +
56+
$"{CsvHelper.QuoteString(info.Hostname)},{CsvHelper.QuoteString(info.IPGeolocationResult?.Info?.Continent)},{CsvHelper.QuoteString(info.IPGeolocationResult?.Info?.Country)}," +
57+
$"{CsvHelper.QuoteString(info.IPGeolocationResult?.Info?.Region)},{CsvHelper.QuoteString(info.IPGeolocationResult?.Info?.City)},{CsvHelper.QuoteString(info.IPGeolocationResult?.Info?.District)}," +
58+
$"{CsvHelper.QuoteString(info.IPGeolocationResult?.Info?.Isp)},{CsvHelper.QuoteString(info.IPGeolocationResult?.Info?.Org)},{CsvHelper.QuoteString(info.IPGeolocationResult?.Info?.As)},{CsvHelper.QuoteString(info.IPGeolocationResult?.Info?.Asname)}," +
59+
$"{info.IPGeolocationResult?.Info?.Hosting},{info.IPGeolocationResult?.Info?.Proxy},{info.IPGeolocationResult?.Info?.Mobile}");
5560

5661
File.WriteAllText(filePath, stringBuilder.ToString());
5762
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace NETworkManager.Utilities;
2+
3+
public static class CsvHelper
4+
{
5+
/// <summary>
6+
/// Wraps a string value in double quotes and escapes any internal double quotes
7+
/// per RFC 4180 (doubles them). Null values are returned as an empty quoted field.
8+
/// </summary>
9+
/// <param name="value">The string value to quote.</param>
10+
/// <returns>The value wrapped in double quotes with internal quotes escaped.</returns>
11+
public static string QuoteString(string? value)
12+
{
13+
return $"\"{value?.Replace("\"", "\"\"")}\"";
14+
}
15+
}

0 commit comments

Comments
 (0)