From 2f7d827beb6c4d672c2b9b898f8b20b4b22ec342 Mon Sep 17 00:00:00 2001 From: Entvex Date: Tue, 19 Oct 2021 13:21:23 +0200 Subject: [PATCH 1/2] The json files now lives in %appdata% Now the settings.json and SavedTunnels.json is saved in %appdata%\NgrokSharp This solves the problem that users experience when starting the application from the commandline. --- FirstTimeWizard.xaml.cs | 2 +- MainWindow.xaml.cs | 27 ++++++++++++++++++--------- ngrokGUI.csproj | 7 +------ 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/FirstTimeWizard.xaml.cs b/FirstTimeWizard.xaml.cs index bcf35ed..ed46528 100644 --- a/FirstTimeWizard.xaml.cs +++ b/FirstTimeWizard.xaml.cs @@ -49,7 +49,7 @@ private void _ngrokManager_DownloadAndUnZipDone(object sender, EventArgs e) private void BtnDownload_OnClick(object sender, RoutedEventArgs e) { - if (btnDownload.Content == "Next") + if ((string)btnDownload.Content == "Next") { tabcl.SelectedIndex = 1; return; diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index f0a456b..823f959 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -16,6 +16,8 @@ public partial class MainWindow : Window { private readonly INgrokManager _ngrokManager; private readonly ObservableCollection _tunnelDescriptions; + private readonly string _downloadFolder = + $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar}NgrokSharp{Path.DirectorySeparatorChar}"; private bool PaidAccount; public MainWindow() @@ -27,11 +29,17 @@ public MainWindow() _ngrokManager = new NgrokManager(); + + if (!File.Exists($"{_downloadFolder}Settings.json")) + { + File.WriteAllText($"{_downloadFolder}Settings.json", "{\r\n \"firstTimeSetupDone\": false\r\n}"); + } + Settings settings; try { //Load settings - settings = JsonConvert.DeserializeObject(File.ReadAllText("Settings.json")); + settings = JsonConvert.DeserializeObject(File.ReadAllText($"{_downloadFolder}Settings.json")); if (settings.FirstTimeSetupDone == false) { @@ -44,17 +52,18 @@ public MainWindow() settings.DataCenterRegion = firstTimeWizard.cmbTunnelExit.SelectedIndex; settings.PaidAccount = (bool) firstTimeWizard.cbxPaidAccount.IsChecked; - File.WriteAllText("Settings.json", JsonConvert.SerializeObject(settings)); + File.WriteAllText($"{_downloadFolder}Settings.json", JsonConvert.SerializeObject(settings)); } } PaidAccount = settings.PaidAccount; - sbStatus.Content = "connected to " + (NgrokManager.Region)settings.DataCenterRegion; + sbStatus.Content = $"connected to {(NgrokManager.Region)settings.DataCenterRegion}"; _ngrokManager.StartNgrok((NgrokManager.Region)settings.DataCenterRegion); - if (File.Exists("SavedTunnels.json")) + if (File.Exists($"{_downloadFolder}SavedTunnels.json")) { - JsonConvert.DeserializeObject>(File.ReadAllText("SavedTunnels.json"))?.ForEach( x => _tunnelDescriptions.Add(x)); + JsonConvert.DeserializeObject>(File.ReadAllText( + $"{_downloadFolder}SavedTunnels.json"))?.ForEach( x => _tunnelDescriptions.Add(x)); } } @@ -139,7 +148,7 @@ private void Window_Closed(object sender, EventArgs e) } } - File.WriteAllText(@"SavedTunnels.json", JsonConvert.SerializeObject(_tunnelDescriptions)); + File.WriteAllText($"{_downloadFolder}SavedTunnels.json", JsonConvert.SerializeObject(_tunnelDescriptions)); } @@ -177,7 +186,7 @@ private void BtnMenuItemRunFirstTimeWizard_OnClick(object sender, RoutedEventArg if (result == MessageBoxResult.Yes) { Settings settings = new Settings {FirstTimeSetupDone = false}; - File.WriteAllText("Settings.json", JsonConvert.SerializeObject(settings)); + File.WriteAllText($"{_downloadFolder}Settings.json", JsonConvert.SerializeObject(settings)); Close(); } } @@ -235,8 +244,8 @@ private async void btnMenuItemDeleteTunnel_OnClick(object sender, RoutedEventArg var result = await _ngrokManager.StopTunnel(_tunnelDescriptions[lwTunnels.SelectedIndex].Name); } - var tunnel = _tunnelDescriptions.SingleOrDefault(x => x.Name == _tunnelDescriptions[lwTunnels.SelectedIndex].Name); - _tunnelDescriptions.Remove(tunnel); + var tunnel = _tunnelDescriptions.SingleOrDefault(x => x.Name == _tunnelDescriptions[lwTunnels.SelectedIndex].Name); + _tunnelDescriptions.Remove(tunnel); } } diff --git a/ngrokGUI.csproj b/ngrokGUI.csproj index 5177704..cf4e344 100644 --- a/ngrokGUI.csproj +++ b/ngrokGUI.csproj @@ -5,6 +5,7 @@ net5.0-windows true icons8-tunnel-256.ico + ngrokGUI @@ -35,10 +36,4 @@ - - - Always - - - \ No newline at end of file From a07ac806d017783cff1be798af2039b5a32a7d86 Mon Sep 17 00:00:00 2001 From: Entvex Date: Tue, 19 Oct 2021 14:02:43 +0200 Subject: [PATCH 2/2] Created folder in %appdata%\NgrokSharp if not already created --- MainWindow.xaml.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 823f959..cf52d2c 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -32,6 +32,7 @@ public MainWindow() if (!File.Exists($"{_downloadFolder}Settings.json")) { + Directory.CreateDirectory(_downloadFolder); File.WriteAllText($"{_downloadFolder}Settings.json", "{\r\n \"firstTimeSetupDone\": false\r\n}"); }