Загрузка данных


#pragma once

using namespace System;
using namespace System::Windows::Forms;
using namespace System::IO;
using namespace System::Text;

namespace PasswordApp {

    public ref class Form1 : public Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
            attempts = 0;
            LoadPassword();
        }

    protected:
        ~Form1()
        {
            if (components)
                delete components;
        }

    private:
        TextBox^ textBox1;
        Button^ button1;
        int attempts;
        String^ savedPassword;
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        void InitializeComponent(void)
        {
            this->textBox1 = (gcnew TextBox());
            this->button1 = (gcnew Button());

            this->SuspendLayout();

            // textBox1
            this->textBox1->Location = Drawing::Point(50, 30);
            this->textBox1->PasswordChar = '*';

            // button1
            this->button1->Location = Drawing::Point(50, 70);
            this->button1->Text = L"Войти";
            this->button1->Click += gcnew EventHandler(this, &Form1::button1_Click);

            // Form1
            this->ClientSize = Drawing::Size(200, 130);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->button1);
            this->Text = L"Введите пароль";

            this->ResumeLayout(false);
            this->PerformLayout();
        }
#pragma endregion

    private:

        // простое "шифрование"
        String^ Encrypt(String^ input)
        {
            String^ result = "";
            for each (wchar_t c in input)
                result += (wchar_t)(c + 2);
            return result;
        }

        String^ Decrypt(String^ input)
        {
            String^ result = "";
            for each (wchar_t c in input)
                result += (wchar_t)(c - 2);
            return result;
        }

        void LoadPassword()
        {
            if (File::Exists("pass.txt"))
            {
                String^ enc = File::ReadAllText("pass.txt");
                savedPassword = Decrypt(enc);
            }
            else
            {
                savedPassword = "1234";
                File::WriteAllText("pass.txt", Encrypt(savedPassword));
            }
        }

        void button1_Click(Object^ sender, EventArgs^ e)
        {
            if (textBox1->Text == savedPassword)
            {
                MessageBox::Show("Пароль верный");

                Form^ f = gcnew Form();
                f->Text = "Заставка программы";

                Button^ changeBtn = gcnew Button();
                changeBtn->Text = "Сменить пароль";
                changeBtn->Location = Drawing::Point(50, 30);

                TextBox^ newPass = gcnew TextBox();
                newPass->Location = Drawing::Point(50, 70);

                changeBtn->Click += gcnew EventHandler(
                    [this, newPass](Object^ s, EventArgs^ e)
                {
                    savedPassword = newPass->Text;
                    File::WriteAllText("pass.txt", Encrypt(savedPassword));
                    MessageBox::Show("Пароль изменён");
                });

                f->Controls->Add(changeBtn);
                f->Controls->Add(newPass);

                f->Show();
                this->Hide();
            }
            else
            {
                attempts++;

                MessageBox::Show("Неверный пароль");

                if (attempts >= 3)
                {
                    MessageBox::Show("Программа закрывается");
                    Application::Exit();
                }
            }
        }
    };
}