PreferencesView.xaml.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Shapes;
  15. namespace WinUI
  16. {
  17. /// <summary>
  18. /// Interaction logic for PreferencesView.xaml
  19. /// </summary>
  20. public partial class PreferencesView : Window
  21. {
  22. public static string AppName = "ZeroTier One";
  23. private RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
  24. private string AppLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
  25. public PreferencesView()
  26. {
  27. InitializeComponent();
  28. string keyValue = rk.GetValue(AppName) as string;
  29. if (keyValue != null && keyValue.Equals(AppLocation))
  30. {
  31. startupCheckbox.IsChecked = true;
  32. }
  33. CentralAPI api = CentralAPI.Instance;
  34. CentralInstanceTextBox.Text = api.Central.ServerURL;
  35. APIKeyTextBox.Text = api.Central.APIKey;
  36. }
  37. private void OKButton_Clicked(object sender, RoutedEventArgs e)
  38. {
  39. CentralAPI api = CentralAPI.Instance;
  40. if (api.Central.ServerURL != CentralInstanceTextBox.Text ||
  41. api.Central.APIKey != APIKeyTextBox.Text)
  42. {
  43. CentralServer newServer = new CentralServer();
  44. newServer.ServerURL = CentralInstanceTextBox.Text;
  45. newServer.APIKey = APIKeyTextBox.Text;
  46. api.Central = newServer;
  47. }
  48. if (startupCheckbox.IsChecked.HasValue && (bool)startupCheckbox.IsChecked)
  49. {
  50. rk.SetValue(AppName, AppLocation);
  51. }
  52. else
  53. {
  54. string keyValue = rk.GetValue(AppName) as string;
  55. if (keyValue != null && keyValue.Equals(AppLocation))
  56. {
  57. rk.DeleteValue(AppName);
  58. }
  59. }
  60. Close();
  61. }
  62. }
  63. }