MainWindowViewModel.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using Avalonia.Controls;
  2. using CommunityToolkit.Mvvm.ComponentModel;
  3. using TestRepeat.Models;
  4. using TestRepeat.Views;
  5. using TestRepeat.Models;
  6. using TestRepeat.ViewModels;
  7. using System.Security.Cryptography;
  8. using System.IO;
  9. using System.Text.RegularExpressions;
  10. using System.Text;
  11. using System;
  12. using TestRepeat.ViewModels.AuthorizationViewModel;
  13. namespace TestRepeat.ViewModels
  14. {
  15. public partial class MainWindowViewModel : ViewModelBase
  16. {
  17. [ObservableProperty] UserControl uc = new Authorization();
  18. public static MainWindowViewModel Instance;
  19. public static _41pKyklevContext Db_context;
  20. public MainWindowViewModel()
  21. {
  22. Instance = this;
  23. Db_context = new _41pKyklevContext();
  24. Initialize();
  25. }
  26. private void Initialize()
  27. {
  28. string userName = Environment.UserName;
  29. string path = Directory.GetCurrentDirectory();
  30. using SHA256Managed hash = new SHA256Managed();
  31. string nameFile = Regex.Replace(Convert.ToBase64String(hash.ComputeHash(Encoding.UTF8.GetBytes(userName))), @"[\/:*?""<>|]", "");
  32. string fullPath = path + "\\" + nameFile + ".bin";
  33. if (File.Exists(fullPath))
  34. {
  35. string[] loginAndPassword = AuthorizationVMAdditionalMethods.ReadFile(fullPath);
  36. if (loginAndPassword != null)
  37. {
  38. AuthorizationViewModel.AuthorizationViewModel auth = new AuthorizationViewModel.AuthorizationViewModel(loginAndPassword[0], loginAndPassword[1]);
  39. }
  40. else
  41. {
  42. string[] filePaths = System.IO.Directory.GetFiles(@path, "*.bin");
  43. foreach (string file in filePaths) {
  44. File.Delete(file);
  45. }
  46. }
  47. }
  48. else
  49. {
  50. string[] filePaths = System.IO.Directory.GetFiles(@path, "*.bin");
  51. foreach (string file in filePaths)
  52. {
  53. File.Delete(file);
  54. }
  55. }
  56. }
  57. }
  58. }