Skip to content

Commit 1197635

Browse files
committed
Added the ability to save "Information", "Text", "Environment" tabs in xml and text formats
1 parent 5fc90f5 commit 1197635

File tree

3 files changed

+70
-27
lines changed

3 files changed

+70
-27
lines changed

WindowTextExtractor/Forms/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.

WindowTextExtractor/Forms/MainForm.cs

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private void MenuItemSaveInformationAsClick(object sender, EventArgs e)
242242
FileName = File.Exists(_informationFileName) ? Path.GetFileName(_informationFileName) : "*.txt",
243243
DefaultExt = "txt",
244244
RestoreDirectory = false,
245-
Filter = "Text Documents (*.txt)|*.txt|All Files (*.*)|*.*"
245+
Filter = "Text Documents (*.txt)|*.txt|XML Documents (*.xml)|*.xml|All Files (*.*)|*.*"
246246
};
247247

248248
if (!File.Exists(_informationFileName))
@@ -253,8 +253,23 @@ private void MenuItemSaveInformationAsClick(object sender, EventArgs e)
253253
if (dialog.ShowDialog() != DialogResult.Cancel)
254254
{
255255
_informationFileName = dialog.FileName;
256-
var content = gvInformation.Tag != null ? ((WindowInformation)gvInformation.Tag).ToString() : string.Empty;
257-
File.WriteAllText(dialog.FileName, content, Encoding.UTF8);
256+
var fileExtension = Path.GetExtension(dialog.FileName).ToLower();
257+
if (fileExtension == ".xml")
258+
{
259+
var information = (WindowInformation)gvInformation.Tag;
260+
var document = new XDocument();
261+
var elements = new XElement("information",
262+
new XElement("cursorInformation", information.CursorDetails.Select(x => new XElement("item", new XAttribute("name", x.Key), new XAttribute("value", x.Value)))),
263+
new XElement("windowInformation", information.WindowDetails.Select(x => new XElement("item", new XAttribute("name", x.Key), new XAttribute("value", x.Value)))),
264+
new XElement("processInformation", information.ProcessDetails.Select(x => new XElement("item", new XAttribute("name", x.Key), new XAttribute("value", x.Value)))));
265+
document.Add(elements);
266+
FileUtils.Save(dialog.FileName, document);
267+
}
268+
else
269+
{
270+
var content = gvInformation.Tag != null ? ((WindowInformation)gvInformation.Tag).ToString() : string.Empty;
271+
File.WriteAllText(dialog.FileName, content, Encoding.UTF8);
272+
}
258273
}
259274
}
260275

@@ -290,10 +305,10 @@ private void MenuItemSaveTextListAsClick(object sender, EventArgs e)
290305
OverwritePrompt = true,
291306
ValidateNames = true,
292307
Title = "Save As",
293-
FileName = File.Exists(_textListFileName) ? Path.GetFileName(_textListFileName) : "*.xml",
294-
DefaultExt = "xml",
308+
FileName = File.Exists(_textListFileName) ? Path.GetFileName(_textListFileName) : "*.txt",
309+
DefaultExt = "txt",
295310
RestoreDirectory = false,
296-
Filter = "XML Documents (*.xml)|*.xml|All Files (*.*)|*.*"
311+
Filter = "Text Documents (*.txt)|*.txt|XML Documents (*.xml)|*.xml|All Files (*.*)|*.*"
297312
};
298313

299314
if (!File.Exists(_textListFileName))
@@ -304,9 +319,18 @@ private void MenuItemSaveTextListAsClick(object sender, EventArgs e)
304319
if (dialog.ShowDialog() != DialogResult.Cancel)
305320
{
306321
_textListFileName = dialog.FileName;
307-
var document = new XDocument();
308-
document.Add(new XElement("items", gvTextList.Rows.OfType<DataGridViewRow>().Select(x => new XElement("item", ((string)x.Cells[0].Value) ?? string.Empty))));
309-
FileUtils.Save(_textListFileName, document);
322+
var fileExtension = Path.GetExtension(dialog.FileName).ToLower();
323+
if (fileExtension == ".xml")
324+
{
325+
var document = new XDocument();
326+
document.Add(new XElement("items", gvTextList.Rows.OfType<DataGridViewRow>().Select(x => new XElement("item", ((string)x.Cells[0].Value) ?? string.Empty))));
327+
FileUtils.Save(dialog.FileName, document);
328+
}
329+
else
330+
{
331+
var content = string.Join($"{Environment.NewLine}{new string('-', 100)}{Environment.NewLine}", gvTextList.Rows.OfType<DataGridViewRow>().Select(x => ((string)x.Cells[0].Value) ?? string.Empty));
332+
File.WriteAllText(dialog.FileName, content, Encoding.UTF8);
333+
}
310334
}
311335
}
312336

@@ -336,7 +360,8 @@ private void MenuItemSaveImageAsClick(object sender, EventArgs e)
336360
fileExtension == ".gif" ? ImageFormat.Gif :
337361
fileExtension == ".jpeg" ? ImageFormat.Jpeg :
338362
fileExtension == ".png" ? ImageFormat.Png :
339-
fileExtension == ".tiff" ? ImageFormat.Tiff : ImageFormat.Wmf;
363+
fileExtension == ".tiff" ? ImageFormat.Tiff :
364+
fileExtension == ".wmf" ? ImageFormat.Wmf : ImageFormat.Bmp;
340365
pbContent.Image.Save(dialog.FileName, imageFormat);
341366
}
342367
}
@@ -351,7 +376,7 @@ private void MenuItemSaveEnvironmentAsClick(object sender, EventArgs e)
351376
FileName = File.Exists(_environmentFileName) ? Path.GetFileName(_environmentFileName) : "*.txt",
352377
DefaultExt = "txt",
353378
RestoreDirectory = false,
354-
Filter = "Text Documents (*.txt)|*.txt|All Files (*.*)|*.*"
379+
Filter = "Text Documents (*.txt)|*.txt|XML Documents (*.xml)|*.xml|All Files (*.*)|*.*"
355380
};
356381

357382
if (!File.Exists(_environmentFileName))
@@ -362,18 +387,31 @@ private void MenuItemSaveEnvironmentAsClick(object sender, EventArgs e)
362387
if (dialog.ShowDialog() != DialogResult.Cancel)
363388
{
364389
_environmentFileName = dialog.FileName;
365-
var content = string.Empty;
366-
if (gvEnvironment.Tag is IDictionary<string, string> variables)
390+
var fileExtension = Path.GetExtension(dialog.FileName).ToLower();
391+
if (fileExtension == ".xml")
367392
{
368-
const int paddingSize = 25;
369-
var builder = new StringBuilder(1024);
370-
foreach (var variableKey in variables.Keys)
393+
if (gvEnvironment.Tag is IDictionary<string, string> variables)
371394
{
372-
builder.AppendLine($"{variableKey,-paddingSize}: {variables[variableKey]}");
395+
var document = new XDocument();
396+
document.Add(new XElement("items", variables.Select(x => new XElement("item", new XAttribute("name", x.Key), new XAttribute("value", x.Value)))));
397+
FileUtils.Save(dialog.FileName, document);
398+
}
399+
}
400+
else
401+
{
402+
var content = string.Empty;
403+
if (gvEnvironment.Tag is IDictionary<string, string> variables)
404+
{
405+
const int paddingSize = 25;
406+
var builder = new StringBuilder(1024);
407+
foreach (var variableKey in variables.Keys)
408+
{
409+
builder.AppendLine($"{variableKey,-paddingSize}: {variables[variableKey]}");
410+
}
411+
content = builder.ToString();
373412
}
374-
content = builder.ToString();
413+
File.WriteAllText(dialog.FileName, content, Encoding.UTF8);
375414
}
376-
File.WriteAllText(dialog.FileName, content, Encoding.UTF8);
377415
}
378416
}
379417

WindowTextExtractor/WindowInformation.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,42 @@ public override string ToString()
3131

3232
if (CursorDetails.Keys.Any())
3333
{
34-
builder.AppendFormat($"Cursor Information {Environment.NewLine}");
34+
builder.Append($"Cursor Information {Environment.NewLine}");
3535
}
3636

3737
foreach (var cursorDetailKey in CursorDetails.Keys)
3838
{
39-
builder.AppendFormat($"{cursorDetailKey.PadRight(paddingSize)}: {CursorDetails[cursorDetailKey]}{Environment.NewLine}");
39+
builder.Append($"{cursorDetailKey.PadRight(paddingSize)}: {CursorDetails[cursorDetailKey]}{Environment.NewLine}");
40+
}
41+
42+
if (CursorDetails.Keys.Any())
43+
{
44+
builder.Append(Environment.NewLine);
4045
}
4146

4247
if (WindowDetails.Keys.Any())
4348
{
44-
builder.AppendFormat($"Window Information {Environment.NewLine}");
49+
builder.Append($"Window Information {Environment.NewLine}");
4550
}
4651

4752
foreach (var windowDetailKey in WindowDetails.Keys)
4853
{
49-
builder.AppendFormat($"{windowDetailKey.PadRight(paddingSize)}: {WindowDetails[windowDetailKey]}{Environment.NewLine}");
54+
builder.Append($"{windowDetailKey.PadRight(paddingSize)}: {WindowDetails[windowDetailKey]}{Environment.NewLine}");
5055
}
5156

5257
if (WindowDetails.Keys.Any())
5358
{
54-
builder.AppendFormat($"{Environment.NewLine}");
59+
builder.Append(Environment.NewLine);
5560
}
5661

5762
if (ProcessDetails.Keys.Any())
5863
{
59-
builder.AppendFormat($"Process Information {Environment.NewLine}");
64+
builder.Append($"Process Information {Environment.NewLine}");
6065
}
6166

6267
foreach (var processDetailKey in ProcessDetails.Keys)
6368
{
64-
builder.AppendFormat($"{processDetailKey.PadRight(paddingSize)}: {ProcessDetails[processDetailKey]}{Environment.NewLine}");
69+
builder.Append($"{processDetailKey.PadRight(paddingSize)}: {ProcessDetails[processDetailKey]}{Environment.NewLine}");
6570
}
6671

6772
return builder.ToString();

0 commit comments

Comments
 (0)