add command-line filename argument, so open-with works

This commit is contained in:
Greg Gauthier 2026-02-15 12:59:43 +00:00
parent a52468f03b
commit c8e70f41bc
2 changed files with 35 additions and 2 deletions

View File

@ -15,7 +15,12 @@ public partial class App : Application
{ {
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{ {
desktop.MainWindow = new MainWindow(); string? fileToOpen = null;
if (desktop.Args?.Length > 0)
{
fileToOpen = desktop.Args[0];
}
desktop.MainWindow = new MainWindow(fileToOpen);
} }
base.OnFrameworkInitializationCompleted(); base.OnFrameworkInitializationCompleted();

View File

@ -17,7 +17,11 @@ namespace NotePad
private bool _isModified; private bool _isModified;
private string? _originalText; private string? _originalText;
public MainWindow() public MainWindow() : this(null)
{
}
public MainWindow(string? fileToOpen)
{ {
InitializeComponent(); InitializeComponent();
UpdateTitle(); UpdateTitle();
@ -26,6 +30,30 @@ namespace NotePad
textBox.TextChanged += OnTextChanged; textBox.TextChanged += OnTextChanged;
Closing += OnWindowClosing; Closing += OnWindowClosing;
if (!string.IsNullOrEmpty(fileToOpen) && File.Exists(fileToOpen))
{
_ = LoadFileAsync(fileToOpen);
}
}
private async Task LoadFileAsync(string filePath)
{
try
{
var text = await File.ReadAllTextAsync(filePath);
_currentFilePath = filePath;
_currentFileName = Path.GetFileName(filePath);
this.FindControl<TextBox>("EditorTextBox")!.Text = text;
_originalText = text;
_isModified = false;
UpdateTitle();
}
catch (Exception)
{
// Silently ignore file loading errors on startup
}
} }
private void UpdateTitle() private void UpdateTitle()