Skip to content

Commit e217506

Browse files
committed
Added file option to open all event logs in a folder
1 parent 95aaf4c commit e217506

3 files changed

Lines changed: 79 additions & 11 deletions

File tree

src/EventLogExpert/MainPage.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<KeyboardAccelerator Modifiers="Ctrl" Key="O" />
1515
</MenuFlyoutItem.KeyboardAccelerators>
1616
</MenuFlyoutItem>
17+
<MenuFlyoutItem Text="Folder" Clicked="OpenFolder_Clicked" />
1718
<MenuFlyoutSubItem Text="Live Event Log" >
1819
<MenuFlyoutItem Text="Application" Clicked="OpenLiveLog_Clicked" />
1920
<MenuFlyoutItem Text="System" Clicked="OpenLiveLog_Clicked" />
@@ -23,6 +24,7 @@
2324
</MenuFlyoutSubItem>
2425
<MenuFlyoutSubItem Text="Add Another Log To This View">
2526
<MenuFlyoutItem Text="File" Clicked="OpenFile_Clicked" CommandParameter="{StaticResource True}" />
27+
<MenuFlyoutItem Text="Folder" Clicked="OpenFolder_Clicked" CommandParameter="{StaticResource True}" />
2628
<MenuFlyoutSubItem Text="Live Event Log" >
2729
<MenuFlyoutItem Text="Application" Clicked="OpenLiveLog_Clicked" CommandParameter="{StaticResource True}" />
2830
<MenuFlyoutItem Text="System" Clicked="OpenLiveLog_Clicked" CommandParameter="{StaticResource True}" />

src/EventLogExpert/MainPage.xaml.cs

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using EventLogExpert.Eventing.EventResolvers;
55
using EventLogExpert.Eventing.Helpers;
66
using EventLogExpert.Eventing.Readers;
7+
using EventLogExpert.Platforms.Windows;
78
using EventLogExpert.Services;
89
using EventLogExpert.UI;
910
using EventLogExpert.UI.Interfaces;
@@ -136,21 +137,38 @@ public void Dispose()
136137
_cancellationTokenSource.Dispose();
137138
}
138139

139-
private static async Task<IEnumerable<FileResult?>> GetFilePickerResult()
140+
private static async Task<IEnumerable<FileResult>> GetFilePickerResultAsync()
140141
{
141142
var options = new PickOptions
142143
{
143144
FileTypes = new FilePickerFileType(
144-
new Dictionary<DevicePlatform, IEnumerable<string>>
145-
{
146-
{ DevicePlatform.WinUI, [".evtx"] }
147-
}
148-
)
145+
new Dictionary<DevicePlatform, IEnumerable<string>> { { DevicePlatform.WinUI, [".evtx"] } })
149146
};
150147

151148
return await FilePicker.Default.PickMultipleAsync(options);
152149
}
153150

151+
private static async Task<IEnumerable<FileResult>> GetFolderPickerResultAsync()
152+
{
153+
string? folderPath = await FolderPickerHelper.PickFolderAsync();
154+
155+
if (folderPath is null)
156+
{
157+
return [];
158+
}
159+
160+
List<FileResult> fileResults = [];
161+
162+
foreach (string file in Directory.EnumerateFiles(folderPath, "*.evtx", SearchOption.TopDirectoryOnly))
163+
{
164+
var fileResult = new FileResult(file);
165+
166+
fileResults.Add(fileResult);
167+
}
168+
169+
return fileResults;
170+
}
171+
154172
private async void CheckForUpdates_Clicked(object? sender, EventArgs e)
155173
{
156174
if (!_currentVersionProvider.IsSupportedOS(DeviceInfo.Version))
@@ -326,21 +344,41 @@ private async void OpenFile_Clicked(object sender, EventArgs e)
326344

327345
bool shouldAddLog = item.CommandParameter is true;
328346

329-
var result = await GetFilePickerResult();
347+
var files = await GetFilePickerResultAsync();
348+
349+
if (!files.Any()) { return; }
350+
351+
if (!shouldAddLog)
352+
{
353+
await _cancellationTokenSource.CancelAsync();
354+
_fluxorDispatcher.Dispatch(new EventLogAction.CloseAll());
355+
}
356+
357+
foreach (var file in files)
358+
{
359+
await OpenLog(file.FullPath, PathType.FilePath, shouldAddLog: true);
360+
}
361+
}
362+
363+
private async void OpenFolder_Clicked(object sender, EventArgs e)
364+
{
365+
if (sender is not MenuFlyoutItem item) { return; }
366+
367+
bool shouldAddLog = item.CommandParameter is true;
330368

331-
var logs = result.Where(f => f is not null).ToList();
369+
var files = await GetFolderPickerResultAsync();
332370

333-
if (logs.Count <= 0) { return; }
371+
if (!files.Any()) { return; }
334372

335373
if (!shouldAddLog)
336374
{
337375
await _cancellationTokenSource.CancelAsync();
338376
_fluxorDispatcher.Dispatch(new EventLogAction.CloseAll());
339377
}
340378

341-
foreach (var file in logs)
379+
foreach (var file in files)
342380
{
343-
await OpenLog(file!.FullPath, PathType.FilePath, shouldAddLog: true);
381+
await OpenLog(file.FullPath, PathType.FilePath, shouldAddLog: true);
344382
}
345383
}
346384

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// // Copyright (c) Microsoft Corporation.
2+
// // Licensed under the MIT License.
3+
4+
using Windows.Storage;
5+
using Windows.Storage.Pickers;
6+
using WinRT.Interop;
7+
8+
namespace EventLogExpert.Platforms.Windows;
9+
10+
public static class FolderPickerHelper
11+
{
12+
public static async Task<string?> PickFolderAsync()
13+
{
14+
FolderPicker picker = new()
15+
{
16+
SuggestedStartLocation = PickerLocationId.Desktop,
17+
FileTypeFilter = { "*" } // Add a wildcard to allow folder selection
18+
};
19+
20+
if (Application.Current?.Windows[0].Handler?.PlatformView is not MauiWinUIWindow window) { return null; }
21+
22+
InitializeWithWindow.Initialize(picker, window.WindowHandle);
23+
24+
StorageFolder? folder = await picker.PickSingleFolderAsync();
25+
26+
return folder?.Path;
27+
}
28+
}

0 commit comments

Comments
 (0)