C#C
C#2y ago
Nadekai

I am lost

For some reason whenever I press enter, nothing happens, the MK_content doesn't get written into the file.

c#
using System;
using System.Collections.Generic;
using System.IO;


namespace Password_Manager
{
    
    public partial class MainWindow : Window
    {
        public bool MK_exists = false;
        public string MK_content = null;
        public string MK_path = "Data\\MasterKey.txt";
        public MainWindow()
        {
            InitializeComponent();

            // Accessing variables
            

            

            if (File.Exists(MK_path))
            {
                // Check if MasterKey.txt exists and has content
                if (new FileInfo(MK_path).Length > 0)
                {
                    MK_exists = true;
                    MK_content = File.ReadAllText(MK_path); // Read the content of MasterKey.txt
                }
            }
            else
            {
                // Create MasterKey.txt if it doesn't exist
                using (File.Create(MK_path)) { }
            }

            // Show the appropriate stack panel based on MK existence
            if (MK_exists)
            {
                sp_MK_Login.Visibility = Visibility.Visible;
            }
            else
            {
                sp_MK_creation.Visibility = Visibility.Visible;
            }

            // Set the content of the password box if MK_content is not null or empty
           

            debug_label.Content = MK_content;

            tb_MK_PasswordBox_Creation.KeyDown += MK_PB_Creation_KeyDown;
        }

        private void MK_PB_Creation_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                if (!string.IsNullOrEmpty(MK_content))
                {

                    tb_MK_PasswordBox_Creation.Password = MK_content;
                    File.WriteAllText(MK_path, MK_content);
                }
            }
        }
    }
}
Was this page helpful?