Skip to content

Commit c0fa81b

Browse files
committed
load/save last config
1 parent 5e23015 commit c0fa81b

File tree

6 files changed

+119
-49
lines changed

6 files changed

+119
-49
lines changed

SqlProfiler/Config.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.IO;
3+
using System.Windows.Forms;
4+
5+
namespace SqlProfiler {
6+
7+
internal class Config {
8+
9+
private static readonly Lazy<Config> instanceField = new Lazy<Config>(Load);
10+
internal static Config Instance => instanceField.Value;
11+
12+
public Config() {
13+
14+
Height = Screen.PrimaryScreen.WorkingArea.Height / 2;
15+
Width = Screen.PrimaryScreen.WorkingArea.Width / 2;
16+
Left = Screen.PrimaryScreen.WorkingArea.Left + Width / 2;
17+
Top = Screen.PrimaryScreen.WorkingArea.Top + Height / 2;
18+
19+
User = "sa";
20+
Password = "sa";
21+
Server = ".";
22+
}
23+
24+
public int Left { get; set; }
25+
public int Top { get; set; }
26+
public int Height { get; set; }
27+
public int Width { get; set; }
28+
29+
public string Server { get; set; }
30+
public string User { get; set; }
31+
public string Password { get; set; }
32+
33+
private static string GetSettingsFilePath() {
34+
return Path.ChangeExtension(Updater.CurrentFileLocation, ".json");
35+
}
36+
37+
internal void Save() {
38+
var fileName = GetSettingsFilePath();
39+
GC.Collect(GC.MaxGeneration);
40+
File.WriteAllText(fileName, this.ToJson());
41+
}
42+
43+
private static Config Load() {
44+
var fileName = GetSettingsFilePath();
45+
try {
46+
if (File.Exists(fileName)) {
47+
var fileData = File.ReadAllText(fileName);
48+
var result = fileData.FromJson<Config>();
49+
if (result != null)
50+
return result;
51+
}
52+
}
53+
catch {
54+
// ignored
55+
}
56+
57+
return new Config();
58+
}
59+
}
60+
}

SqlProfiler/MainForm.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SqlProfiler/MainForm.cs

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Data;
44
using System.Data.SqlClient;
5+
using System.Drawing;
56
using System.Globalization;
67
using System.IO;
78
using System.Text;
@@ -47,9 +48,6 @@ private enum ProfilingStateEnum {
4748
private readonly List<ListViewItem> m_Cached = new List<ListViewItem>(1024);
4849
private readonly List<ListViewItem> m_CachedUnFiltered = new List<ListViewItem>(1024);
4950
private readonly Dictionary<string, ListViewItem> m_itembysql = new Dictionary<string, ListViewItem>();
50-
private string m_servername = "";
51-
private string m_username = "";
52-
private string m_userpassword = "";
5351
internal int lastpos = -1;
5452
internal string lastpattern = "";
5553
private ListViewNF lvEvents;
@@ -71,19 +69,45 @@ public MainForm() {
7169
Text = $"SqlProfiler {(Environment.Is64BitProcess ? "x64" : "x32")} - {Updater.CurrentVersion}";
7270
StartPosition = FormStartPosition.CenterScreen;
7371
edPassword.TextBox.PasswordChar = '*';
72+
7473
m_currentsettings = TraceProperties.TraceSettings.GetDefaultSettings();
75-
m_servername = ".";
76-
m_username = "sa";
7774
ParseCommandLine();
7875
InitLV();
79-
edServer.Text = m_servername;
80-
edUser.Text = m_username;
81-
edPassword.Text = m_userpassword;
82-
tbAuth.SelectedIndex = String.IsNullOrEmpty(m_username) ? 0 : 1;
76+
edServer.Text = Config.Instance.Server;
77+
edUser.Text = Config.Instance.User;
78+
edPassword.Text = Config.Instance.Password;
79+
tbAuth.SelectedIndex = String.IsNullOrEmpty(Config.Instance.User) ? 0 : 1;
8380
if (m_autostart) RunProfiling(false);
8481
UpdateButtons();
82+
83+
MinimumSize = new Size(1100, 666);
84+
85+
Load += (sender, args) => {
86+
Left = Config.Instance.Left;
87+
Top = Config.Instance.Top;
88+
Height = Config.Instance.Height;
89+
Width = Config.Instance.Width;
90+
};
91+
92+
Closing += (sender, args) => {
93+
Config.Instance.Left = Left;
94+
Config.Instance.Top = Top;
95+
Config.Instance.Height = Height;
96+
Config.Instance.Width = Width;
97+
98+
Config.Instance.Save();
99+
};
100+
}
101+
102+
public sealed override Size MinimumSize {
103+
get { return base.MinimumSize; }
104+
set { base.MinimumSize = value; }
85105
}
86106

107+
private string GetConfigFilePath() {
108+
return Path.ChangeExtension(Updater.CurrentFileLocation, ".json");
109+
}
110+
87111
//DatabaseName = Filters.DatabaseName,
88112
//LoginName = Filters.LoginName,
89113
//HostName = Filters.HostName,
@@ -149,17 +173,17 @@ private void ParseCommandLine() {
149173
switch (args[i].ToLower()) {
150174
case "-s":
151175
case "-server":
152-
m_servername = ep;
176+
Config.Instance.Server = ep;
153177
i++;
154178
break;
155179
case "-u":
156180
case "-user":
157-
m_username = ep;
181+
Config.Instance.User = ep;
158182
i++;
159183
break;
160184
case "-p":
161185
case "-password":
162-
m_userpassword = ep;
186+
Config.Instance.Password = ep;
163187
i++;
164188
break;
165189
case "-m":
@@ -212,8 +236,8 @@ private void ParseCommandLine() {
212236
i++;
213237
}
214238

215-
if (m_servername.Length == 0) {
216-
m_servername = @".\sqlexpress";
239+
if (Config.Instance.Server.Length == 0) {
240+
Config.Instance.Server = @".\sqlexpress";
217241
}
218242
}
219243
catch (Exception e) {
@@ -253,7 +277,6 @@ private void UpdateButtons() {
253277
edPassword.Enabled = edServer.Enabled && (tbAuth.SelectedIndex == 1);
254278
}
255279

256-
257280
private void InitLV() {
258281
lvEvents = new ListViewNF {
259282
Dock = DockStyle.Fill,
@@ -272,7 +295,6 @@ private void InitLV() {
272295
AllowColumnReorder = false
273296
};
274297
lvEvents.RetrieveVirtualItem += lvEvents_RetrieveVirtualItem;
275-
lvEvents.KeyDown += lvEvents_KeyDown;
276298
lvEvents.ItemSelectionChanged += listView1_ItemSelectionChanged_1;
277299
lvEvents.ColumnClick += lvEvents_ColumnClick;
278300
lvEvents.SelectedIndexChanged += lvEvents_SelectedIndexChanged;
@@ -688,8 +710,8 @@ private void StartProfiling() {
688710
m_itembysql.Clear();
689711
lvEvents.VirtualListSize = 0;
690712
StartProfilerThread();
691-
m_servername = edServer.Text;
692-
m_username = edUser.Text;
713+
Config.Instance.Server = edServer.Text;
714+
Config.Instance.User = edUser.Text;
693715
}
694716
catch (Exception e) {
695717
MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
@@ -815,7 +837,6 @@ private void PauseProfiling() {
815837
UpdateButtons();
816838
}
817839

818-
819840
internal void SelectAllEvents(bool select) {
820841
lock (m_Cached) {
821842
lvEvents.BeginUpdate();
@@ -833,13 +854,6 @@ internal void SelectAllEvents(bool select) {
833854
}
834855
}
835856

836-
private void lvEvents_KeyDown(object sender, KeyEventArgs e) {
837-
}
838-
839-
private void MainForm_Load(object sender, EventArgs e) {
840-
}
841-
842-
843857
private void timer1_Tick(object sender, EventArgs e) {
844858
Queue<ProfilerEvent> saved;
845859
Exception exc;
@@ -988,7 +1002,6 @@ private void extractSelectedEventsToolStripMenuItem_Click(object sender, EventAr
9881002
CopyEventsToClipboard(true);
9891003
}
9901004

991-
9921005
private void pauseTraceToolStripMenuItem_Click(object sender, EventArgs e) {
9931006
PauseProfiling();
9941007
}
@@ -1050,7 +1063,6 @@ private void selectAllToolStripMenuItem_Click(object sender, EventArgs e) {
10501063
// MessageBox.Show(String.Format("Failed to find \"{0}\". Searched to the end of data. ", lastpattern), "SqlProfiler", MessageBoxButtons.OK, MessageBoxIcon.Information);
10511064
//}
10521065

1053-
10541066
internal void PerformFind(bool forwards, bool wrapAround) {
10551067
if (String.IsNullOrEmpty(lastpattern)) return;
10561068
int lastpos = lvEvents.Items.IndexOf(lvEvents.FocusedItem);
@@ -1089,7 +1101,6 @@ internal void PerformFind(bool forwards, bool wrapAround) {
10891101
"SqlProfiler", MessageBoxButtons.OK, MessageBoxIcon.Information);
10901102
}
10911103

1092-
10931104
private void ShowSelectedEvent() {
10941105
int focusedIndex = lvEvents.Items.IndexOf(lvEvents.FocusedItem);
10951106
if ((focusedIndex > -1) && (focusedIndex < m_Cached.Count)) {
@@ -1103,7 +1114,6 @@ private void ShowSelectedEvent() {
11031114
}
11041115
}
11051116

1106-
11071117
private bool FindText(int i) {
11081118
ListViewItem lvi = m_Cached[i];
11091119
ProfilerEvent evt = (ProfilerEvent) lvi.Tag;
@@ -1320,7 +1330,6 @@ private void keepSelectedToolStripMenuItem_Click(object sender, EventArgs e) {
13201330
lvEvents.SelectedIndices.Clear();
13211331
}
13221332

1323-
13241333
private void SaveToExcelXmlFile() {
13251334
XmlDocument doc = new XmlDocument();
13261335
XmlProcessingInstruction pi = doc.CreateProcessingInstruction("mso-application", "progid='Excel.Sheet'");
@@ -1450,7 +1459,6 @@ private void SaveToExcelXmlFile() {
14501459
}
14511460
}
14521461

1453-
14541462
private void SetFilterEvents() {
14551463
if (m_CachedUnFiltered.Count == 0) {
14561464
lvEvents.SelectedIndices.Clear();
@@ -1475,7 +1483,6 @@ private void SetFilterEvents() {
14751483
}
14761484
}
14771485

1478-
14791486
private void ClearFilterEvents() {
14801487
if (m_CachedUnFiltered.Count > 0) {
14811488
m_Cached.Clear();
@@ -1488,30 +1495,21 @@ private void ClearFilterEvents() {
14881495
}
14891496
}
14901497

1491-
14921498
private void saveAllEventsToExcelXmlFileToolStripMenuItem_Click(object sender, EventArgs e) {
14931499
SaveToExcelXmlFile();
14941500
}
14951501

1496-
/// <summary>
1497-
/// Persist the server string when it changes.
1498-
/// </summary>
1499-
/// <param name="sender"></param>
1500-
/// <param name="e"></param>
15011502
private void edServer_TextChanged(object sender, EventArgs e) {
1502-
m_servername = edServer.Text;
1503+
Config.Instance.Server = edServer.Text;
15031504
}
15041505

1505-
1506-
/// <summary>
1507-
/// Persist the user name string when it changes.
1508-
/// </summary>
1509-
/// <param name="sender"></param>
1510-
/// <param name="e"></param>
15111506
private void edUser_TextChanged(object sender, EventArgs e) {
1512-
m_username = edUser.Text;
1507+
Config.Instance.User = edUser.Text;
15131508
}
15141509

1510+
private void edPassword_TextChanged(object sender, EventArgs e) {
1511+
Config.Instance.Password = edPassword.Text;
1512+
}
15151513

15161514
private void filterCapturedEventsToolStripMenuItem_Click(object sender, EventArgs e) {
15171515
SetFilterEvents();

SqlProfiler/SqlProfiler.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Reference Include="System.Xml"/>
5454
</ItemGroup>
5555
<ItemGroup>
56+
<Compile Include="Config.cs" />
5657
<Compile Include="ListViewExtensions.cs"/>
5758
<Compile Include="TextDataComparer.cs"/>
5859
<Compile Include="EventList.cs"/>

SqlProfiler/TraceProperties.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ public class TraceSettings {
8181
BatchCompleted = true,
8282
RPCCompleted = true,
8383
StartTime = true,
84-
EndTime = true
84+
EndTime = true,
85+
#if DEBUG
86+
ApplicationName = true,
87+
DatabaseName = true,
88+
HostName = true,
89+
ObjectName = true,
90+
#endif
8591
};
8692

8793
public TraceFilters Filters = new TraceFilters {

SqlProfiler/Updater.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ internal static T FromJson<T>(this string json) {
6969
return serializer.Deserialize<T>(json);
7070
}
7171

72+
internal static string ToJson(this object value) {
73+
var serializer = new JavaScriptSerializer();
74+
return serializer.Serialize(value);
75+
}
76+
7277
internal static void CheckForUpdates(bool silent) {
7378
string newVersion;
7479
string newVersionUrl = null;

0 commit comments

Comments
 (0)