Skip to content

Commit d80bfd9

Browse files
committed
* Added export methods
1 parent b84c851 commit d80bfd9

5 files changed

Lines changed: 182 additions & 8 deletions

File tree

Advanced PortChecker/Advanced PortChecker.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<Generator>MSBuild:Compile</Generator>
6363
<SubType>Designer</SubType>
6464
</ApplicationDefinition>
65+
<Compile Include="Classes\ExportWriter.cs" />
6566
<Compile Include="Classes\LvCheck.cs" />
6667
<Compile Include="Classes\OperationInformation.cs" />
6768
<Compile Include="Classes\PortChecker.cs" />
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
}

Advanced PortChecker/Windows/MainWindow.xaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
<MenuItem.Icon>
2727
<Image Width="16" Height="16" Source="../Resources/Images/file.png"/>
2828
</MenuItem.Icon>
29-
<MenuItem Header="Export as TXT"/>
30-
<MenuItem Header="Export as HTML"/>
31-
<MenuItem Header="Export as CSV"/>
29+
<MenuItem Header="Export as TXT" Click="BtnExportAsText_Click"/>
30+
<MenuItem Header="Export as HTML" Click="BtnExportAsHtml_Click"/>
31+
<MenuItem Header="Export as CSV" Click="BtnExportAsCsv_Click"/>
3232
<Separator></Separator>
33-
<MenuItem Header="Export as..."/>
33+
<MenuItem Header="Export as..." Click="BtnExportAs_Click"/>
3434
</MenuItem>
3535
<Separator></Separator>
3636
<MenuItem Header="Exit" Click="Exit_Click">

Advanced PortChecker/Windows/MainWindow.xaml.cs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Windows;
4-
using System.Windows.Controls;
54
using System.Windows.Shell;
65
using Advanced_PortChecker.Classes;
6+
using Microsoft.Win32;
77

88
namespace Advanced_PortChecker.Windows
99
{
@@ -19,7 +19,6 @@ public partial class MainWindow
1919

2020
#endregion
2121

22-
2322
public MainWindow()
2423
{
2524
_updateManager = new UpdateManager("http://codedead.com/Software/Advanced%20PortChecker/update.xml");
@@ -206,8 +205,56 @@ private void BtnCopy_Click(object sender, RoutedEventArgs e)
206205
{
207206
if (LvPorts.SelectedItems.Count == 0) return;
208207

209-
LvCheck selected = (LvCheck)LvPorts.SelectedItems[0];
208+
LvCheck selected = (LvCheck) LvPorts.SelectedItems[0];
210209
Clipboard.SetText(selected.Address + " " + selected.Port + " " + selected.Type + " " + selected.Description);
211210
}
211+
212+
private void BtnExportAs_Click(object sender, RoutedEventArgs e)
213+
{
214+
if (LvPorts.Items.Count == 0) return;
215+
216+
SaveFileDialog sfd = new SaveFileDialog { Filter = "Text file (*.txt)|*.txt|HTML file (*.html)|*.html|CSV file (*.csv)|*.csv" };
217+
if (sfd.ShowDialog() != true) return;
218+
219+
switch (sfd.FilterIndex)
220+
{
221+
default:
222+
ExportWriter.SaveAsText(sfd.FileName, LvPorts);
223+
break;
224+
case 2:
225+
ExportWriter.SaveAsHTML(sfd.FileName, LvPorts);
226+
break;
227+
case 3:
228+
ExportWriter.SaveAsCSV(sfd.FileName, LvPorts);
229+
break;
230+
}
231+
}
232+
233+
private void BtnExportAsText_Click(object sender, RoutedEventArgs e)
234+
{
235+
if (LvPorts.Items.Count == 0) return;
236+
237+
SaveFileDialog sfd = new SaveFileDialog { Filter = "Text file (*.txt)|*.txt" };
238+
if (sfd.ShowDialog() != true) return;
239+
ExportWriter.SaveAsText(sfd.FileName, LvPorts);
240+
}
241+
242+
private void BtnExportAsHtml_Click(object sender, RoutedEventArgs e)
243+
{
244+
if (LvPorts.Items.Count == 0) return;
245+
246+
SaveFileDialog sfd = new SaveFileDialog { Filter = "HTML file (*.html)|*.html" };
247+
if (sfd.ShowDialog() != true) return;
248+
ExportWriter.SaveAsHTML(sfd.FileName, LvPorts);
249+
}
250+
251+
private void BtnExportAsCsv_Click(object sender, RoutedEventArgs e)
252+
{
253+
if (LvPorts.Items.Count == 0) return;
254+
255+
SaveFileDialog sfd = new SaveFileDialog { Filter = "CSV file (*.csv)|*.csv" };
256+
if (sfd.ShowDialog() != true) return;
257+
ExportWriter.SaveAsCSV(sfd.FileName, LvPorts);
258+
}
212259
}
213260
}

Advanced PortChecker/Windows/SettingsWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<Label Grid.Row="1" Grid.Column="0" Content="Metro brush:"></Label>
5151
<syncfusion:ColorPicker x:Name="CpMetroBrush" Grid.Row="1" Grid.Column="1" />
5252
<Label Grid.Row="2" Grid.Column="0">Border thickness:</Label>
53-
<syncfusion:IntegerTextBox x:Name="IntBorderThickness" Grid.Row="2" Grid.Column="1" MinValue="1"></syncfusion:IntegerTextBox>
53+
<syncfusion:IntegerTextBox x:Name="IntBorderThickness" Grid.Row="2" Grid.Column="1" MinValue="0"></syncfusion:IntegerTextBox>
5454
</Grid>
5555
<Grid Grid.Row="2">
5656
<Grid.ColumnDefinitions>

0 commit comments

Comments
 (0)