diff --git a/ShowEditorCommand.cs b/ShowEditorCommand.cs index 212fdc6..7d0511a 100644 --- a/ShowEditorCommand.cs +++ b/ShowEditorCommand.cs @@ -30,6 +30,7 @@ public class ShowEditorCommand : PSCmdlet private bool _matchCase; private bool _matchWholeWord; #endregion + private List _allowedFileTypes = new List(); [Parameter(ParameterSetName = "Path", ValueFromPipeline = true, Position = 0)] public string Path { get; set; } @@ -43,6 +44,12 @@ protected override void BeginProcessing() _runspace = RunspaceFactory.CreateRunspace(); _runspace.Open(); currentDirectory = SessionState.Path.CurrentLocation.Path; + // populate the allowed file types for dialogs on startup + _allowedFileTypes.Add(".ps1"); + _allowedFileTypes.Add(".psm1"); + _allowedFileTypes.Add(".psd1"); + _allowedFileTypes.Add(".json"); + _allowedFileTypes.Add(".txt"); } protected override void ProcessRecord() @@ -245,13 +252,7 @@ private void Open() { return; } - List allowedFileTypes = new List(); - allowedFileTypes.Add(".ps1"); - allowedFileTypes.Add(".psm1"); - allowedFileTypes.Add(".psd1"); - allowedFileTypes.Add(".json"); - allowedFileTypes.Add(".txt"); - var dialog = new OpenDialog("Open file", "", allowedFileTypes); + var dialog = new OpenDialog("Open file", "", _allowedFileTypes); dialog.CanChooseDirectories = false; dialog.CanChooseFiles = true; dialog.AllowsMultipleSelection = false; @@ -405,13 +406,7 @@ private bool Save(bool saveAs) { if (string.IsNullOrEmpty(Path) || saveAs) { - List allowedFileTypes = new List(); - allowedFileTypes.Add(".ps1"); - allowedFileTypes.Add(".psm1"); - allowedFileTypes.Add(".psd1"); - allowedFileTypes.Add(".json"); - allowedFileTypes.Add(".txt"); - var dialog = new SaveDialog(saveAs ? "Save file as" : "Save file", "", allowedFileTypes); + var dialog = new SaveDialog(saveAs ? "Save file as" : "Save file", "", _allowedFileTypes); dialog.DirectoryPath = currentDirectory; Application.Run(dialog);