diff --git a/NotePad/App.axaml.cs b/NotePad/App.axaml.cs index 90cd562..fc732d8 100644 --- a/NotePad/App.axaml.cs +++ b/NotePad/App.axaml.cs @@ -15,7 +15,12 @@ public partial class App : Application { 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(); diff --git a/NotePad/MainWindow.axaml.cs b/NotePad/MainWindow.axaml.cs index 1c3da27..6500854 100644 --- a/NotePad/MainWindow.axaml.cs +++ b/NotePad/MainWindow.axaml.cs @@ -17,7 +17,11 @@ namespace NotePad private bool _isModified; private string? _originalText; - public MainWindow() + public MainWindow() : this(null) + { + } + + public MainWindow(string? fileToOpen) { InitializeComponent(); UpdateTitle(); @@ -26,6 +30,30 @@ namespace NotePad textBox.TextChanged += OnTextChanged; 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("EditorTextBox")!.Text = text; + _originalText = text; + _isModified = false; + UpdateTitle(); + } + catch (Exception) + { + // Silently ignore file loading errors on startup + } } private void UpdateTitle()